知って得するUnity エディタ拡張編

Click here to load reader

  • date post

    21-May-2015
  • Category

    Technology

  • view

    19.485
  • download

    1

Embed Size (px)

Transcript of 知って得するUnity エディタ拡張編

  • 1. Unity http://baba-s.hatenablog.com/

2. 1. PropertyDrawerUnity 2. AssetPostprocessor 3. MenuItem 3. PropertyDrawer Unity 4. CustomEditor PropertyDrawer 5. PropertyDrawer RangeAttribute MultilineAttribute 6. RangeAttribute using UnityEngine; public class Character : MonoBehaviour { [Range(1, 100)] public int Level = 1; } 7. MultilineAttribute using UnityEngine; public class Character : MonoBehaviour { [Multiline(3)] public string Comment; } 8. PropertyDrawer RangeAttributeMultilineAttribute PropertyDrawer 9. PropertyDrawer PropertyDrawer using UnityEngine; public class Character : MonoBehaviour { [MyProperty("")] public string Name; } 10. using UnityEngine; public class MyPropertyAttribute : PropertyAttribute { public string Label; public MyPropertyAttribute(string label) { Label = label; } } 1.PropertyAttribute 11. using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(MyPropertyAttribute))] public class MyPropertyDrawer : PropertyDrawer { } 2.PropertyDrawer 12. public override void OnGUI( Rect position, SerializedProperty property, GUIContent label) { var myPropertyAttribute = attribute as MyPropertyAttribute; property.stringValue = EditorGUI.TextField( position, myPropertyAttribute.Label, property.stringValue); } 3. OnGUI 13. using UnityEngine; public class Character : MonoBehaviour { [MyProperty("")] public string Name; } 4.PropertyDrawer 14. PropertyDrawer PropertyDrawer Editor #if UNITY_EDITOR #endif 15. MyPropertyAttribute.cs MyPropertyAttribute Gist 1. MyPropertyAttribute.cs https://gist.github.com/baba-s/9430324 2. MyPropertyDrawer.cs https://gist.github.com/baba-s/9430335 3. MyPropertyAttribute.cs Unity 4. MyPropertyDrawer.cs UnityEditor 16. PropertyDrawer https://github.com/anchan828/property-drawer-collection http://blogs.unity3d.com/2012/09/07/property-drawers-in-unity-4/ PropertyDrawer CompactAttribute EnumLabelAttribute PopupAttribute SceneNameAttribute RegexAttribute 17. using UnityEngine; public class Character : MonoBehaviour { public Vector3 Position; } Vector2Vector3 18. CompactAttribute.cs 1. CompactAttribute.cs https://github.com/anchan828/property-drawer-collection 2. CompactAttribute.csUnity 19. using UnityEngine; public class Character : MonoBehaviour { [Compact] public Vector3 Position; } CompactAttribute 20. using UnityEngine; public class Character : MonoBehaviour { public enum JobType { SOLDIER, SORCERER, } public JobType Job; } 21. EnumLabelAttribute.cs 1. EnumLabelAttribute.cs https://github.com/anchan828/property-drawer-collection 2. EnumLabelAttribute.csUnity 22. using UnityEngine; public class Character : MonoBehaviour { public enum JobType { [EnumLabel("")] SOLDIER, [EnumLabel("")] SORCERER, } [EnumLabel("")] public JobType Job; } EnumLabelAttribute 23. using UnityEngine; public class Character : MonoBehaviour { public string Job; public int Money; } 24. PopupAttribute.cs 1. PopupAttribute.cs https://github.com/anchan828/property-drawer-collection 2. PopupAttribute.csUnity 25. using UnityEngine; public class Character : MonoBehaviour { [Popup("", "")] public string Job; [Popup(1, 5, 10, 50, 100, 500)] public int Money; } PopupAttribute 26. using UnityEngine; public class LoadSceneButton : MonoBehaviour { public string SceneName; } 27. SceneNameAttribute.cs 1. SceneNameAttribute.cs https://github.com/anchan828/property-drawer-collection 2. SceneNameAttribute.csUnity 28. using UnityEngine; public class LoadSceneButton : MonoBehaviour { [SceneName] public string SceneName; } SceneNameAttribute 29. using UnityEngine; public class Config : MonoBehaviour { // IP public string ServerAddress; } 30. RegexAttribute.cs 1. RegexAttribute.cs https://gist.github.com/baba-s/9430471 2. RegexAttribute.csUnity 31. using UnityEngine; public class Config : MonoBehaviour { // IP [Regex(@"^(?:d{1,3}.){3}d{1,3}$", "IP!:'127.0.0.1'")] public string ServerAddress; } RegexAttribute 32. PropertyDrawer PropertyDrawer 33. AssetPostprocessor 34. AssetPostprocessor Unity AudioClip Unity 35. AssetPostprocessor AssetPostprocessor 36. 1.AssetPostprocessor using UnityEditor; using UnityEngine; public class MyAssetPostprocessor : AssetPostprocessor { } 37. 2. // 1 // 1 true // 2 false private static bool IsOneByteStr(string str) { var bytes = System.Text.Encoding.GetEncoding(932).GetBytes(str); return bytes.Length == str.Length; } http://7ujm.net/NET%20Framework/isOneByteChar.html 38. 3. OnPostprocessAllAssets // // importedAssets : // deletedAssets : // movedAssets : // movedFromPath: private static void OnPostprocessAllAssets( string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPath) { } 39. 4.AssetDatabase.DeleteAsset foreach (var importedAsset in importedAssets) { if (!IsOneByteStr(importedAsset)) { // if (AssetDatabase.DeleteAsset(importedAsset)) { Debug.Log(importedAsset + ""); } } } 40. MyAssetPostprocessor.cs MyAssetPostprocessor Gist 1. MyAssetPostprocessor.cs https://gist.github.com/baba-s/9426854 2. MyAssetPostprocessor.cs UnityEditor 41. MenuItem 42. MenuItem Unity 43. MenuItem MenuItem 44. using UnityEditor; public static class MyClassCreator { // Unity // [MenuItem("Tools/Create My Class")] private static void Create() { } } 1.MenuItem 45. // var builder = new System.Text.StringBuilder(); builder.AppendLine("public class MyClass"); builder.AppendLine("{"); builder.AppendLine("}"); 2.StringBuilder 46. // // System.IO.File.WriteAllText( "Assets/MyClass.cs", builder.ToString(), System.Text.Encoding.UTF8); 3.File.WriteAllText 47. // Unity AssetDatabase.Refresh( ImportAssetOptions.ImportRecursive); 4.AssetDatabase.Refresh 48. 5.Unity public class MyClass { } 49. MyClassCreator.cs MyClassCreator Gist 1. MyClassCreator.cs https://gist.github.com/baba-s/9446762 2. MyClassCreator.cs UnityEditor 50. MenuItem 51. 52. EditorBuildSettings.scenes foreach (var n in EditorBuildSettings.scenes) { Debug.Log(n); } 53. 1. SceneNameCreator.cs https://gist.github.com/baba-s/9286120 2. SceneNameCreator.csEditor 3. UnityTools>Create>Scene Name 4. SceneName.cs 54. /// /// /// public static class SceneName { public const string Title = "Title"; public const string MainMenu = "MainMenu"; } 55. // Application.LoadLevel(SceneName.Title); // Application.LoadLevel("Title"); 56. 57. InternalEditorUtility.tags foreach (var n in InternalEditorUtility.tags) { Debug.Log(n); } 58. 1. TagNameCreator.cs https://gist.github.com/baba-s/9287103 2. TagNameCreator.csEditor 3. UnityTools>Create>Tag Name 4. TagName.cs 59. /// /// /// public static class TagName { public const string Untagged = "Untagged"; public const string Respawn = "Respawn"; public const string Finish = "Finish"; public const string EditorOnly = "EditorOnly"; public const string MainCamera = "MainCamera"; public const string Player = "Player"; public const string GameController = "GameController"; public const string Character = "Character"; } 60. // if (gameObject.CompareTag(TagName.Character)) { // ... } // if (gameObject.CompareTag("Character")) { // ... } 61. 62. InternalEditorUtility.layers foreach (var n in InternalEditorUtility.layers) { Debug.Log(n); } 63. 1. LayerNameCreator.cs https://gist.github.com/baba-s/9286212 2. LayerNameCreator.csEditor 3. UnityTools>Create>Layer Name 4. LayerName.cs 64. /// /// /// public static class LayerName { public const int Default = 0; public const int TransparentFX = 1; public const int IgnoreRaycast = 2; public const int Water = 4; public const int Character = 8; public const int DefaultMask = 1; public const int TransparentFXMask = 2; public const int IgnoreRaycastMask = 4; public const int WaterMask = 16; public const int CharacterMask = 256; } 65. // gameObject.layer = LayerName.Character; // gameObject.layer = 8; 66. MenuItem MenuItem Unity API 67.