using System; using System.Linq; using System.Collections.Generic; using UnityEngine; namespace UnityEditor.Graphs.Material { public enum PreviewState { Full = 0, Selected = 1, Off = 2 } public class MaterialProperties : ScriptableObject, IGenerateGraphProperties { private Vector2 m_ScrollPos; [SerializeField] List m_ShaderProperties = new List (); [SerializeField] public int previewState; //static GUIContent[] previewOptions; public event EventHandler OnChangePreviewState; /*static MaterialProperties () { previewOptions = Enum.GetNames (typeof (PreviewState)).Select (x => new GUIContent (x)).ToArray (); }*/ public void OnEnable () { if (OnChangePreviewState != null) OnChangePreviewState ((PreviewState)previewState, null); } public void DoGUI (List nodes) { //m_ScrollPos1 = BeginArea ("Options", m_ScrollPos1); /*GUILayout.BeginHorizontal (); GUILayout.Label ("Preview", EditorStyles.largeLabel); EditorGUI.BeginChangeCheck (); previewState = EditorGUILayout.CycleButton (previewState, previewOptions, EditorStyles.miniButton); if (EditorGUI.EndChangeCheck () && OnChangePreviewState != null) OnChangePreviewState ((PreviewState)previewState, null); GUILayout.EndHorizontal ();*/ //EndArea (); m_ScrollPos = BeginArea ("Exposed Properties", m_ScrollPos); if (GUILayout.Button ("Add Property")) AddProperty (); var propsToRemove = new List (); foreach (var property in m_ShaderProperties) { // property changed bool nameChanged; bool valuesChanged; property.PropertyOnGUI (out nameChanged, out valuesChanged); if (nameChanged || valuesChanged) { foreach (var node in nodes.Where(x => x is PropertyNode).Cast()) { node.RefreshBoundProperty (property, nameChanged); } foreach (var node in nodes.Where(x => x is UVNode).Cast()) { node.RefreshBoundProperty (property, nameChanged); } } if (GUILayout.Button ("Remove")) { propsToRemove.Add (property); } EditorGUILayout.Separator (); } EndArea (); foreach (var prop in propsToRemove) { foreach (var node in nodes.Where(x => x is PropertyNode).Cast()) { node.UnbindProperty (prop); } foreach (var node in nodes.Where(x => x is UVNode).Cast()) { node.UnbindProperty (prop); } m_ShaderProperties.Remove (prop); } } private void AddProperty() { var menu = new GenericMenu (); menu.AddItem (new GUIContent ("Texture"), false, CreateProperty); menu.AddItem (new GUIContent ("Color"), false, CreateProperty); menu.AddItem (new GUIContent ("Vector"), false, CreateProperty); menu.ShowAsContext (); } private void CreateProperty () where T : ShaderProperty { var createdProperty = CreateInstance (); createdProperty.name = typeof(T).Name; createdProperty.hideFlags = HideFlags.HideInHierarchy; m_ShaderProperties.Add (createdProperty); AssetDatabase.AddObjectToAsset (createdProperty, this); } static Vector2 BeginArea (string title, Vector2 scrollPos) { DoHeader (title); scrollPos = GUILayout.BeginScrollView (scrollPos); GUILayout.BeginVertical (); return scrollPos; } static void EndArea () { GUILayout.EndVertical (); GUILayout.EndScrollView(); } static void DoHeader (string title) { GUILayout.BeginHorizontal (/*EditorStyles.inspectorBig*/); GUILayout.BeginVertical (); GUILayout.Label (title, EditorStyles.largeLabel); GUILayout.EndVertical (); GUILayout.EndHorizontal (); } public IEnumerable GetPropertiesForPropertyType(PropertyType propertyType) { return m_ShaderProperties.Where(x => x.propertyType == propertyType); } public void GenerateSharedProperties (PropertyGenerator shaderProperties, ShaderGenerator propertyUsages, GenerationMode generationMode) { foreach (var property in m_ShaderProperties) { property.GeneratePropertyBlock (shaderProperties, generationMode); property.GeneratePropertyUsages (propertyUsages, generationMode); } } } }