O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

【Unity】Scriptable object 入門と活用例

8.223 visualizações

Publicada em

ScriptableObjectとはどういった物か、ScriptableObjectを利用すると何が良いのかといった基本的な内容から、実際にScriptableObjectを活用した開発の効率化について紹介します。

このスライドは『ヴァルキリーコネコクト』のクオリティを支える開発技術最前線で講演した内容のスライドです。 http://www.a-tm.co.jp/recruit/news/event-6944/

Publicada em: Tecnologia

【Unity】Scriptable object 入門と活用例

  1. 1. 

  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7. 
 

  8. 8. 
 
 

  9. 9.
  10. 10.
  11. 11. 
 
 
 
 
 
 
 
 
 
 
 

  12. 12. 
 
 
 
 
 
 
 
 
 
 
 
 
 

  13. 13. 
 
 
 
 
 
 
 

  14. 14. 
 
 
 
 
 
 
 

  15. 15. 
 
 
 
 
 

  16. 16.
  17. 17.
  18. 18.
  19. 19.
  20. 20. [CreateAssetMenu]
 public class TankConfig : ScriptableObject
 {
 public float speed;
 public float rotate;
 }

  21. 21. 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);
 }
 }

  22. 22. public class TankController : MonoBehaviour
 {
 [SerializeField] TankAIBase ai;
 
 public void Update ()
 {
 ai.Do(gameObject);
 }
 } public abstract class TankAIBase : ScriptableObject
 {
 public virtual void Do (GameObject obj) { }
 }
  23. 23. [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);
 }
 }

  24. 24. [CreateAssetMenu]
 public class TankMove : TankAIBase
 {
 public float speed = 0.5f;
 
 public override void Do (GameObject obj)
 {
 obj.GetComponent<NavMeshAgent>().Move(obj.transform.forward * speed);
 }
 }
  25. 25. 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); }
 }

  26. 26. [DefaultExecutionOrder (-100)]
 public class EventTriggerBehaviour : MonoBehaviour
 {
 [SerializeField]
 UnityEvent onAwake = new UnityEvent (), onDestroy = new UnityEvent ();
 
 void Awake ()
 {
 onAwake.Invoke ();
 }
 
 void OnDestroy ()
 {
 onDestroy.Invoke ();
 }
 }

  27. 27.
  28. 28. [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; // レイアウト記述 } }
  29. 29. [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;
 }
 }
  30. 30. [CreateAssetMenu]
 public class Data : ScriptableObject
 {
 [SerializeField] int count;
 
 void OnValidate ()
 {
 count = Mathf.Clamp (count, 0, 99);
 }
 }
  31. 31.
  32. 32. 
 
 

  33. 33. 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); } }
  34. 34. [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);
 }
 }
 } 

  35. 35. public abstract class ResettableScriptableObject : ScriptableObject
 {
 public abstract void Reset ();
 }
 [CreateAssetMenu]
 public class SaveData : ResettableScriptableObject
 { public int count = 0; 
 public override void Reset ()
 { //初期化コード count = 0; } }

  36. 36. public class DataResetter : MonoBehaviour
 {
 public ResettableScriptableObject[] resettableScriptableObjects;
 
 private void Awake ()
 {
 for (int i = 0; i < resettableScriptableObjects.Length; i++)
 {
 resettableScriptableObjects[i].Reset ();
 }
 }
 }

  37. 37. public abstract class ResettableScriptableObject : ScriptableObject
 {
 [SerializeField] int _count = 0;
 public int count{get; set;}
 
 void OnEnable()
 {
 count = _count;
 }
 }

  38. 38. public class ResettableScriptableObject : ScriptableObject 
 {
 protected virtual void OnEnable()
 {
 #if UNITY_EDITOR
 if( EditorApplication.isPlayingOrWillChangePlaymode == true ){
 UnityEditor.EditorApplication.playModeStateChanged += (state) => {
 if( EditorApplication.isPlayingOrWillChangePlaymode == false) {
 Resources.UnloadAsset(this);
 }
 } ;
 }
 #endif
 }
 }

  39. 39. [CreateAssetMenu]
 public class Config : ResettableScriptableObject
 {
 public float _count;
 }

  40. 40. 
 
 
 
 
 
 
 
 
 

  41. 41.
  42. 42. 
 

  43. 43.
  44. 44.
  45. 45.
  46. 46.
  47. 47.

×