Created
August 17, 2015 16:45
-
-
Save edom18/39e98a2babeb29fd9dd6 to your computer and use it in GitHub Desktop.
[Unity] ScriptableObjectを使ってModelを作る ref: http://qiita.com/edo_m18/items/d09ccd8f167e6b1ecf71
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class SampleModel : ScriptableObject { | |
public delegate void ChangePointHandler(int point); | |
public event ChangePointHandler OnChangePoint; | |
public int Point { get; set; } | |
void OnEnable() { | |
Debug.Log("////////// Initialize model //////////"); | |
Point = 0; | |
} | |
public void AddPoint(int point) { | |
Point += point; | |
if (OnChangePoint != null) { | |
OnChangePoint(Point); | |
} | |
} | |
public void ShowPoint() { | |
Debug.Log(Point); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
[InitializeOnLoad] | |
public class CreateScriptAsset { | |
static CreateScriptAsset() { | |
// SampleModelを生成 | |
SampleModel model = ScriptableObject.CreateInstance<SampleModel>(); | |
// ユニークなパスを生成する | |
// (多分、重複する名前がある場合、自動的に(2)とかつけてくれるやつ) | |
string path = AssetDatabase.GenerateUniqueAssetPath("Assets/Model/" + typeof(SampleModel) + ".asset"); | |
Debug.Log("Path: " + path); | |
// 該当パスにオブジェクトアセットを生成 | |
AssetDatabase.CreateAsset(model, path); | |
// 未保存のアセットをアセットデータベースに保存 | |
AssetDatabase.SaveAssets(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class ModelGetter : MonoBehaviour { | |
public SampleModel model; | |
void Start() { | |
model.OnChangePoint += OnChange; | |
} | |
// Update is called once per frame | |
void Update () { | |
if (Input.GetMouseButtonDown(0)) { | |
model.AddPoint(1); | |
// model.ShowPoint(); | |
if (model.Point > 10) { | |
model.Point = 5; | |
Application.LoadLevel("Sub"); | |
} | |
} | |
} | |
void OnChange(int point) { | |
Debug.Log("Event was receirved: " + point); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment