Created
April 30, 2015 13:49
-
-
Save s2kw/4d3a3fa3947feaa19591 to your computer and use it in GitHub Desktop.
serializeProperty経由で書き込む時の注意
This file contains hidden or 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; | |
[CustomEditor( typeof( XXX ) )] | |
[CanEditMultipleObjects] | |
public class XXXInspector : Editor { | |
SerializedProperty xxx;// public float xxx{private get{return this.__xxx;}set{this.__xxx = value;}} [SerializeField] float __xxx; | |
SerializedProperty yyy;// [SerializeField]private float yyy; | |
SerializedProperty zzz;// [SerializeField]private Transform zzz; // reference type. | |
public void OnEnable(){ | |
this.xxx = serializedObject.FindProperty( "xxx" ) ; | |
this.yyy = serializedObject.FindProperty( "yyy" ) ; | |
this.zzz = serializedObject.FindProperty( "zzz" ) ; | |
} | |
public override void OnInspectorGUI(){ | |
serializedObject.Update(); | |
var script = target as XXX; | |
# region property of xxx | |
if( this.xxx == null ){ | |
Debug.LogError( "xxx is null"); | |
}else{ | |
// スクリプトのsetter経由で値を渡す。普通に動く。 | |
script.xxx = EditorGUILayout.Slider( "xxx", this.xxx.floatValue, 0.0001f, 1f ) ; | |
} | |
# endregion | |
# region property of yyy | |
if( speed == null ){ | |
Debug.LogError("yyy is null"); | |
}else{ | |
// こやつが曲者。動かないスライダになる。 | |
this.yyy.floatValue = EditorGUILayout.Slider( "yyy", this.yyy.floatValue, 0.001f, 1f ) ; | |
} | |
# endregion | |
# region property of zzz | |
if( this.zzz == null ){ | |
Debug.LogError("zzz is null"); | |
}else{ | |
this.zzz.objectReferenceValue = EditorGUILayout.ObjectField( "zzz", this.zzz.objectReferenceValue, typeof( Transform ), true ); | |
} | |
# endregion | |
if( GUILayout.Button( "Hoge!" ) && Application.isPlaying ){ | |
script.Hoge(); | |
} | |
// これがないとyyyは動かない。xxxとzzzは動く。 | |
serializedObject.ApplyModifiedProperties(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment