Skip to content

Instantly share code, notes, and snippets.

@edom18
Created August 17, 2015 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edom18/39e98a2babeb29fd9dd6 to your computer and use it in GitHub Desktop.
Save edom18/39e98a2babeb29fd9dd6 to your computer and use it in GitHub Desktop.
[Unity] ScriptableObjectを使ってModelを作る ref: http://qiita.com/edo_m18/items/d09ccd8f167e6b1ecf71
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);
}
}
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();
}
}
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