|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using UnityEditor; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace Cinemachine.Editor |
| 7 | +{ |
| 8 | + [CustomEditor(typeof(CinemachineBasicMultiChannelPerlin))] |
| 9 | + internal sealed class CinemachineBasicMultiChannelPerlinEditor |
| 10 | + : BaseEditor<CinemachineBasicMultiChannelPerlin> |
| 11 | + { |
| 12 | + List<NoiseSettings> mNoisePresets; |
| 13 | + string[] mNoisePresetNames; |
| 14 | + SerializedProperty m_Profile; |
| 15 | + |
| 16 | + protected override List<string> GetExcludedPropertiesInInspector() |
| 17 | + { |
| 18 | + List<string> excluded = base.GetExcludedPropertiesInInspector(); |
| 19 | + excluded.Add(FieldPath(x => x.m_NoiseProfile)); |
| 20 | + return excluded; |
| 21 | + } |
| 22 | + |
| 23 | + private void OnEnable() |
| 24 | + { |
| 25 | + m_Profile = FindProperty(x => x.m_NoiseProfile); |
| 26 | + RebuildProfileList(); |
| 27 | + } |
| 28 | + |
| 29 | + void RebuildProfileList() |
| 30 | + { |
| 31 | + mNoisePresets = FindAssetsByType<NoiseSettings>(); |
| 32 | +#if UNITY_2018_1_OR_NEWER |
| 33 | + if (ScriptableObjectUtility.CinemachineIsPackage) |
| 34 | + AddAssetsFromDirectory( |
| 35 | + mNoisePresets, |
| 36 | + ScriptableObjectUtility.CinemachineInstallAssetPath + "/Presets/Noise"); |
| 37 | +#endif |
| 38 | + mNoisePresets.Insert(0, null); |
| 39 | + List<string> presetNameList = new List<string>(); |
| 40 | + foreach (var n in mNoisePresets) |
| 41 | + presetNameList.Add((n == null) ? "(none)" : n.name); |
| 42 | + mNoisePresetNames = presetNameList.ToArray(); |
| 43 | + } |
| 44 | + |
| 45 | + public override void OnInspectorGUI() |
| 46 | + { |
| 47 | + BeginInspector(); |
| 48 | + |
| 49 | + if (m_Profile.objectReferenceValue == null) |
| 50 | + EditorGUILayout.HelpBox( |
| 51 | + "A Noise Profile is required. You may choose from among the NoiseSettings assets defined in the project.", |
| 52 | + MessageType.Warning); |
| 53 | + |
| 54 | + Rect rect = EditorGUILayout.GetControlRect(true); |
| 55 | + float iconSize = rect.height + 4; |
| 56 | + rect.width -= iconSize; |
| 57 | + int preset = mNoisePresets.IndexOf((NoiseSettings)m_Profile.objectReferenceValue); |
| 58 | + preset = EditorGUI.Popup(rect, "Noise Profile", preset, mNoisePresetNames); |
| 59 | + NoiseSettings newProfile = preset < 0 ? null : mNoisePresets[preset]; |
| 60 | + if ((NoiseSettings)m_Profile.objectReferenceValue != newProfile) |
| 61 | + { |
| 62 | + m_Profile.objectReferenceValue = newProfile; |
| 63 | + serializedObject.ApplyModifiedProperties(); |
| 64 | + } |
| 65 | + rect.x += rect.width; rect.width = iconSize; rect.height = iconSize; |
| 66 | + if (GUI.Button(rect, EditorGUIUtility.IconContent("_Popup"), GUI.skin.label)) |
| 67 | + { |
| 68 | + GenericMenu menu = new GenericMenu(); |
| 69 | + if (m_Profile.objectReferenceValue != null) |
| 70 | + { |
| 71 | + menu.AddItem(new GUIContent("Edit"), false, () => Selection.activeObject = m_Profile.objectReferenceValue); |
| 72 | + menu.AddItem(new GUIContent("Clone"), false, () => |
| 73 | + { |
| 74 | + m_Profile.objectReferenceValue = CreateProfile( |
| 75 | + (NoiseSettings)m_Profile.objectReferenceValue); |
| 76 | + RebuildProfileList(); |
| 77 | + serializedObject.ApplyModifiedProperties(); |
| 78 | + }); |
| 79 | + menu.AddItem(new GUIContent("Locate"), false, () => EditorGUIUtility.PingObject(m_Profile.objectReferenceValue)); |
| 80 | + } |
| 81 | + menu.AddItem(new GUIContent("New"), false, () => |
| 82 | + { |
| 83 | + //Undo.RecordObject(Target, "Change Noise Profile"); |
| 84 | + m_Profile.objectReferenceValue = CreateProfile(null); |
| 85 | + RebuildProfileList(); |
| 86 | + serializedObject.ApplyModifiedProperties(); |
| 87 | + }); |
| 88 | + menu.ShowAsContext(); |
| 89 | + } |
| 90 | + |
| 91 | + DrawRemainingPropertiesInInspector(); |
| 92 | + } |
| 93 | + |
| 94 | + public static List<T> FindAssetsByType<T>() where T : UnityEngine.Object |
| 95 | + { |
| 96 | + List<T> assets = new List<T>(); |
| 97 | + string[] guids = AssetDatabase.FindAssets(string.Format("t:{0}", typeof(T))); |
| 98 | + for (int i = 0; i < guids.Length; i++) |
| 99 | + { |
| 100 | + string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]); |
| 101 | + T asset = AssetDatabase.LoadAssetAtPath<T>(assetPath); |
| 102 | + if (asset != null) |
| 103 | + { |
| 104 | + assets.Add(asset); |
| 105 | + } |
| 106 | + } |
| 107 | + return assets; |
| 108 | + } |
| 109 | + |
| 110 | + static void AddAssetsFromDirectory<T>(List<T> assets, string path) where T : UnityEngine.Object |
| 111 | + { |
| 112 | + try |
| 113 | + { |
| 114 | + var info = new DirectoryInfo(path); |
| 115 | + var fileInfo = info.GetFiles(); |
| 116 | + foreach (var file in fileInfo) |
| 117 | + { |
| 118 | + string name = path + "/" + file.Name; |
| 119 | + T a = AssetDatabase.LoadAssetAtPath(name, typeof(T)) as T; |
| 120 | + if (a != null) |
| 121 | + assets.Add(a); |
| 122 | + } |
| 123 | + } |
| 124 | + catch |
| 125 | + { |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + NoiseSettings CreateProfile(NoiseSettings copyFrom) |
| 130 | + { |
| 131 | + var path = string.Empty; |
| 132 | + var scene = Target.gameObject.scene; |
| 133 | + if (string.IsNullOrEmpty(scene.path)) |
| 134 | + path = "Assets/"; |
| 135 | + else |
| 136 | + { |
| 137 | + var scenePath = Path.GetDirectoryName(scene.path); |
| 138 | + var extPath = scene.name + "_Profiles"; |
| 139 | + var profilePath = scenePath + "/" + extPath; |
| 140 | + if (!AssetDatabase.IsValidFolder(profilePath)) |
| 141 | + AssetDatabase.CreateFolder(scenePath, extPath); |
| 142 | + path = profilePath + "/"; |
| 143 | + } |
| 144 | + |
| 145 | + var profile = ScriptableObject.CreateInstance<NoiseSettings>(); |
| 146 | + if (copyFrom != null) |
| 147 | + profile.CopyFrom(copyFrom); |
| 148 | + path += Target.VirtualCamera.Name + " Noise.asset"; |
| 149 | + path = AssetDatabase.GenerateUniqueAssetPath(path); |
| 150 | + AssetDatabase.CreateAsset(profile, path); |
| 151 | + AssetDatabase.SaveAssets(); |
| 152 | + AssetDatabase.Refresh(); |
| 153 | + return profile; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments