62. public class TankController : MonoBehaviour
{
[SerializeField] TankConfig config;
public void Update ()
{
var v = Input.GetAxis ("Vertical1");
var x = Input.GetAxis ("Horizontal1");
if (v != 0)
GetComponent<NavMeshAgent> ().Move (transform.forward * (v * config.speed));
if (x != 0)
transform.localRotation *= Quaternion.AngleAxis (x * config.rotate, Vector3.up);
}
}
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73. public class TankController : MonoBehaviour
{
[SerializeField] TankAIBase ai;
public void Update ()
{
ai.Do(gameObject);
}
}
public abstract class TankAIBase : ScriptableObject
{
public virtual void Do (GameObject obj) { }
}
74. [CreateAssetMenu]
public class TanksStand : TankAIBase{}
[CreateAssetMenu]
public class TankRotate : TankAIBase
{
public float rotate = 2;
public override void Do (GameObject obj)
{
obj.transform.localRotation *= Quaternion.AngleAxis (rotate, Vector3.up);
}
}
75. [CreateAssetMenu]
public class TankMove : TankAIBase
{
public float speed = 0.5f;
public override void Do (GameObject obj)
{
obj.GetComponent<NavMeshAgent>().Move(obj.transform.forward * speed);
}
}
76.
77.
78.
79.
80. public class TankController : MonoBehaviour
{
public void Move(float accel)
{
GetComponent<NavMeshAgent>().Move(transform.forward * (accel * 0.2f));
}
}
[CreateAssetMenu]
public class TankData : ScriptableObject
{
public TankController tankController{get; set;}
public void Up () { tankController.Move(5); }
public void Down () { tankController.Move(-5); }
}
81. [DefaultExecutionOrder (-100)]
public class EventTriggerBehaviour : MonoBehaviour
{
[SerializeField]
UnityEvent onAwake = new UnityEvent (), onDestroy = new UnityEvent ();
void Awake ()
{
onAwake.Invoke ();
}
void OnDestroy ()
{
onDestroy.Invoke ();
}
}
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93. [CreateAssetMenu]
public class Config : ScriptableObject
{
public Item[] items = new Item[0];
[System.Serializable]
public class Item
{
public Texture texture;
public int param;
public string name;
}
}
[CustomEditor (typeof(Config))]
public class ConfigEditor : Editor
{
public override void OnInspectorGUI ()
{
base.OnInspectorGUI();
var config = (Config)target;
// レイアウト記述
}
}
94. [CreateAssetMenu]
public class Config : ScriptableObject
{
public Item[] items = new Item[0];
[System.Serializable]
public class Item
{
[PreviewTexture]
public Texture texture;
[Range(0, 100)]
public int param;
public string name;
}
}
95. [CreateAssetMenu]
public class Data : ScriptableObject
{
[SerializeField] int count;
void OnValidate ()
{
count = Mathf.Clamp (count, 0, 99);
}
}
96.
97.
98.
99.
100. public class param1_importer : AssetPostprocessor
{
static void OnPostprocessAllAssets (
string[] importedAssets, string[] deletedAssets, string[] movedAssets,
string[] movedFromAssetPaths)
{
ExcelItem data = (ExcelItem)AssetDatabase.LoadAssetAtPath (exportPath, typeof(ExcelItem));
if (data == null) {
data = ScriptableObject.CreateInstance<ExcelItem> ();
AssetDatabase.CreateAsset ((ScriptableObject)data, exportPath);
}
// インポート処理
ScriptableObject obj = AssetDatabase.LoadAssetAtPath (
exportPath, typeof(ScriptableObject)) as ScriptableObject;
EditorUtility.SetDirty (obj);
}
}
101.
102.
103.
104.
105.
106.
107.
108.
109.
110. [CreateAssetMenu]
public class SampleDictionary : ScriptableObject, ISerializationCallbackReceiver
{
const string path = "item.json";
public Item item = new Item ();
public void OnBeforeSerialize ()
{
var json = JsonUtility.ToJson (item);
File.WriteAllText (path, json);
}
public void OnAfterDeserialize ()
{
if (File.Exists (path)) {
var json = File.ReadAllText (path);
JsonUtility.FromJsonOverwrite (json, item);
}
}
}
111.
112.
113. public abstract class ResettableScriptableObject : ScriptableObject
{
public abstract void Reset ();
}
[CreateAssetMenu]
public class SaveData : ResettableScriptableObject
{
public int count = 0;
public override void Reset ()
{
//初期化コード
count = 0;
}
}
114. public class DataResetter : MonoBehaviour
{
public ResettableScriptableObject[] resettableScriptableObjects;
private void Awake ()
{
for (int i = 0; i < resettableScriptableObjects.Length; i++)
{
resettableScriptableObjects[i].Reset ();
}
}
}
115. public abstract class ResettableScriptableObject : ScriptableObject
{
[SerializeField] int _count = 0;
public int count{get; set;}
void OnEnable()
{
count = _count;
}
}