├── LICENSE ├── MaterialInstancedProperties.cs ├── MaterialInstancedPropertiesEditor.cs ├── README.md └── Screenshot.png /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 James Dimick 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MaterialInstancedProperties.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | 4 | namespace Jick 5 | { 6 | [ExecuteInEditMode,RequireComponent(typeof(MeshRenderer)),AddComponentMenu("Jick/Material Instanced Properties"),DisallowMultipleComponent] 7 | public class MaterialInstancedProperties : MonoBehaviour 8 | { 9 | [SerializeField] public List Properties = new List(); 10 | [HideInInspector] private MeshRenderer _Mesh; 11 | [HideInInspector] private MaterialPropertyBlock _MatProps; 12 | 13 | private void Awake() 14 | { 15 | SetMaterialPropertyBlock(); 16 | } 17 | 18 | private void OnDestroy() 19 | { 20 | if ( _Mesh == null ) 21 | { 22 | _Mesh = GetComponent(); 23 | } 24 | 25 | if ( _Mesh != null ) 26 | { 27 | _Mesh.SetPropertyBlock( null ); 28 | } 29 | } 30 | 31 | #if UNITY_EDITOR 32 | private void Update() 33 | { 34 | SetMaterialPropertyBlock(); 35 | } 36 | #endif 37 | 38 | private void SetMaterialPropertyBlock() 39 | { 40 | if ( _Mesh == null || _MatProps == null ) 41 | { 42 | _Mesh = GetComponent(); 43 | _MatProps = new MaterialPropertyBlock(); 44 | _Mesh.GetPropertyBlock( _MatProps ); 45 | } 46 | 47 | if ( _MatProps != null ) 48 | { 49 | if ( Properties != null && Properties.Count > 0 ) 50 | { 51 | _MatProps.Clear(); 52 | 53 | foreach ( MaterialProperty property in Properties ) 54 | { 55 | if ( property.Change ) 56 | { 57 | switch ( property.Type ) 58 | { 59 | case "Color": 60 | _MatProps.SetColor( property.Name, property.ColorValue ); 61 | break; 62 | 63 | case "Texture": 64 | if ( property.TextureValue != null ) _MatProps.SetTexture( property.Name, property.TextureValue ); 65 | break; 66 | 67 | case "Float": 68 | _MatProps.SetFloat( property.Name, property.FloatValue ); 69 | break; 70 | 71 | case "Vector": 72 | _MatProps.SetVector( property.Name, property.VectorValue ); 73 | break; 74 | } 75 | } 76 | } 77 | 78 | _Mesh.SetPropertyBlock( _MatProps ); 79 | } 80 | } 81 | } 82 | } 83 | 84 | [System.Serializable] 85 | public class MaterialProperty 86 | { 87 | [SerializeField] public string Name = ""; 88 | [SerializeField] public string Desc = ""; 89 | [SerializeField] public string Type = ""; 90 | [SerializeField] public bool Change = false; 91 | [SerializeField] public Color ColorValue = Color.white; 92 | [SerializeField] public Texture TextureValue = null; 93 | [SerializeField] public float FloatValue = 0f; 94 | [SerializeField] public Vector4 VectorValue = Vector4.zero; 95 | 96 | public MaterialProperty( string name, string desc, Color value ) 97 | { 98 | Name = name; 99 | Desc = desc; 100 | Type = "Color"; 101 | ColorValue = value; 102 | } 103 | 104 | public MaterialProperty( string name, string desc, Texture value ) 105 | { 106 | Name = name; 107 | Desc = desc; 108 | Type = "Texture"; 109 | TextureValue = value; 110 | } 111 | 112 | public MaterialProperty( string name, string desc, float value ) 113 | { 114 | Name = name; 115 | Desc = desc; 116 | Type = "Float"; 117 | FloatValue = value; 118 | } 119 | 120 | public MaterialProperty( string name, string desc, Vector4 value ) 121 | { 122 | Name = name; 123 | Desc = desc; 124 | Type = "Vector"; 125 | VectorValue = value; 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /MaterialInstancedPropertiesEditor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using UnityEngine; 6 | using UnityEditor; 7 | using UnityEditor.Experimental.Rendering; 8 | using SPT = UnityEditor.ShaderUtil.ShaderPropertyType; 9 | 10 | namespace Jick 11 | { 12 | [CustomEditor(typeof(MaterialInstancedProperties))] 13 | public class MaterialInstancedPropertiesEditor : Editor 14 | { 15 | private MaterialInstancedProperties _CastedTarget; 16 | private SerializedProperty _Properties; 17 | private Material _Material; 18 | private Shader _Shader; 19 | 20 | private void OnEnable() 21 | { 22 | _CastedTarget = (MaterialInstancedProperties)target; 23 | _Properties = serializedObject.FindProperty( "Properties" ); 24 | 25 | if ( _CastedTarget.GetComponent() != null && _CastedTarget.GetComponent().sharedMaterial != null ) 26 | { 27 | _Material = _CastedTarget.GetComponent().sharedMaterial; 28 | _Shader = _Material.shader; 29 | } 30 | 31 | PopulatePropertiesFromShader(); 32 | } 33 | 34 | public override void OnInspectorGUI() 35 | { 36 | serializedObject.Update(); 37 | 38 | if ( _Properties == null ) 39 | { 40 | _Properties = serializedObject.FindProperty( "Properties" ); 41 | } 42 | 43 | if ( _Properties != null && _Shader != null ) 44 | { 45 | if ( _Properties.arraySize > 0 ) 46 | { 47 | List finalProps = new List(); 48 | for ( int i = 0; i < _Properties.arraySize; i++ ) 49 | { 50 | SerializedProperty prop = _Properties.GetArrayElementAtIndex( i ); 51 | 52 | if ( prop != null && prop.FindPropertyRelative( "Name" ) != null ) 53 | { 54 | if ( ShaderUtilExtensions.HasShaderProperty( _Shader, prop.FindPropertyRelative( "Name" ).stringValue ) ) 55 | { 56 | finalProps.Add( prop ); 57 | } 58 | } 59 | } 60 | finalProps = finalProps.OrderBy( x => ShaderUtilExtensions.GetShaderPropertyNames( _Shader ).IndexOf( x.FindPropertyRelative( "Name" ).stringValue ) ).ToList(); 61 | 62 | EditorGUILayout.Space(); 63 | 64 | for ( int j = 0; j < finalProps.Count; j++ ) 65 | { 66 | SerializedProperty prop = finalProps[j]; 67 | string name = prop.FindPropertyRelative( "Name" ).stringValue; 68 | string desc = prop.FindPropertyRelative( "Desc" ).stringValue; 69 | string type = prop.FindPropertyRelative( "Type" ).stringValue; 70 | bool change = prop.FindPropertyRelative( "Change" ).boolValue; 71 | string[] attrs = ShaderUtilExtensions.GetShaderPropertyAttributes( _Shader, name ) ?? new string[0]; 72 | SerializedProperty p = prop.FindPropertyRelative( $"{type}Value" ); 73 | GUIContent label = new GUIContent( desc, name ); 74 | 75 | EditorGUILayout.BeginHorizontal(); 76 | 77 | EditorGUI.BeginChangeCheck(); 78 | var overrideRect = GUILayoutUtility.GetRect( 17f, 17f, GUILayout.ExpandWidth( false ) ); 79 | overrideRect.yMin += 4f; 80 | bool chnge = GUI.Toggle( overrideRect, change, CoreEditorUtils.GetContent( "|Override this property for this object." ), CoreEditorStyles.smallTickbox ); 81 | if ( EditorGUI.EndChangeCheck() ) 82 | { 83 | prop.FindPropertyRelative( "Change" ).boolValue = chnge; 84 | } 85 | 86 | EditorGUI.BeginDisabledGroup( !change ); 87 | 88 | switch ( type ) 89 | { 90 | case "Color": 91 | EditorGUI.BeginChangeCheck(); 92 | Color valColor = EditorGUILayout.ColorField( label, p.colorValue, true, true, true ); 93 | if ( EditorGUI.EndChangeCheck() ) 94 | { 95 | p.colorValue = valColor; 96 | } 97 | break; 98 | 99 | case "Texture": 100 | EditorGUI.BeginChangeCheck(); 101 | Texture valTexture = (Texture) EditorGUILayout.ObjectField( label, p.objectReferenceValue, typeof( Texture ), false ); 102 | if ( EditorGUI.EndChangeCheck() ) 103 | { 104 | p.objectReferenceValue = valTexture; 105 | } 106 | break; 107 | 108 | case "Float": 109 | EditorGUI.BeginChangeCheck(); 110 | float valFloat = attrs.Contains( "Toggle", StringComparer.OrdinalIgnoreCase ) ? Convert.ToSingle( EditorGUILayout.Toggle( label, 111 | Convert.ToBoolean( Mathf.Clamp01( p.floatValue ) ) ) ) : EditorGUILayout.FloatField( label, p.floatValue ); 112 | if ( EditorGUI.EndChangeCheck() ) 113 | { 114 | p.floatValue = valFloat; 115 | } 116 | break; 117 | 118 | case "Vector": 119 | EditorGUI.BeginChangeCheck(); 120 | Vector4 valVector = EditorGUILayout.Vector4Field( label, p.vector4Value ); 121 | if ( EditorGUI.EndChangeCheck() ) 122 | { 123 | p.vector4Value = valVector; 124 | } 125 | break; 126 | } 127 | 128 | EditorGUI.EndDisabledGroup(); 129 | 130 | EditorGUILayout.EndHorizontal(); 131 | } 132 | } 133 | } 134 | 135 | serializedObject.ApplyModifiedProperties(); 136 | } 137 | 138 | private void PopulatePropertiesFromShader() 139 | { 140 | if ( _Shader != null ) 141 | { 142 | int propertyCount = ShaderUtil.GetPropertyCount( _Shader ); 143 | 144 | if ( propertyCount > 0 ) 145 | { 146 | for ( int i = 0; i < propertyCount; i++ ) 147 | { 148 | string name = ShaderUtil.GetPropertyName( _Shader, i ); 149 | 150 | if ( !_CastedTarget.Properties.Any( x => x.Name == name ) ) 151 | { 152 | string desc = ShaderUtil.GetPropertyDescription( _Shader, i ); 153 | 154 | MaterialProperty newProp; 155 | switch ( ShaderUtil.GetPropertyType( _Shader, i ) ) 156 | { 157 | case SPT.Color: 158 | newProp = new MaterialProperty( name, desc, _Material.GetColor( name ) ); 159 | break; 160 | 161 | case SPT.TexEnv: 162 | newProp = new MaterialProperty( name, desc, _Material.GetTexture( name ) ); 163 | break; 164 | 165 | case SPT.Vector: 166 | newProp = new MaterialProperty( name, desc, _Material.GetVector( name ) ); 167 | break; 168 | 169 | case SPT.Float: 170 | case SPT.Range: 171 | default: 172 | newProp = new MaterialProperty( name, desc, _Material.GetFloat( name ) ); 173 | break; 174 | } 175 | 176 | _CastedTarget.Properties.Add( newProp ); 177 | } 178 | } 179 | } 180 | } 181 | } 182 | } 183 | 184 | public static class ShaderUtilExtensions 185 | { 186 | private delegate string[] GetShaderPropertyAttributesDelegate( Shader s, string name ); 187 | private static GetShaderPropertyAttributesDelegate _getShaderPropertyAttributes; 188 | 189 | /// 190 | /// Returns an array of strings representing every attribute on the given property of the given . 191 | /// 192 | public static string[] GetShaderPropertyAttributes( Shader s, string name ) 193 | { 194 | if ( _getShaderPropertyAttributes == null ) 195 | { 196 | Type type = typeof( ShaderUtil ); 197 | MethodInfo methodInfo = type.GetMethod( "GetShaderPropertyAttributes", BindingFlags.Static | BindingFlags.NonPublic ); 198 | 199 | _getShaderPropertyAttributes = (GetShaderPropertyAttributesDelegate)Delegate.CreateDelegate( typeof( GetShaderPropertyAttributesDelegate ), methodInfo ); 200 | } 201 | 202 | return _getShaderPropertyAttributes( s, name ); 203 | } 204 | 205 | /// 206 | /// Returns a of all properties in the given . 207 | /// 208 | public static List GetShaderPropertyNames( Shader s ) 209 | { 210 | List output = new List(); 211 | int count = ShaderUtil.GetPropertyCount( s ); 212 | 213 | if ( count > 0 ) 214 | { 215 | for ( int i = 0; i < count; i++ ) 216 | { 217 | output.Add( ShaderUtil.GetPropertyName( s, i ) ); 218 | } 219 | } 220 | 221 | return output; 222 | } 223 | 224 | /// 225 | /// Returns whether the given contains a property matching the given name. 226 | /// 227 | public static bool HasShaderProperty( Shader s, string name ) 228 | { 229 | for ( int i = 0; i < ShaderUtil.GetPropertyCount( s ); i++ ) 230 | { 231 | if ( ShaderUtil.GetPropertyName( s, i ) == name ) 232 | { 233 | return true; 234 | } 235 | } 236 | 237 | return false; 238 | } 239 | } 240 | } 241 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity Material Instanced Properties Editor 2 | A generic editor for manipulating instanced properties of materials in Unity. 3 | 4 | ![Screenshot of the editor in Unity](Screenshot.png?raw=true) 5 | 6 | # Unity Version 7 | It was created in Unity **2018.3.0** but it should work fine in older versions _(unsure how far back)_. Please submit a report if you find any issues. 8 | 9 | # Limitations 10 | * There is currently no way to tell between an instanced material property and a non-instanced material property on the C# side in Unity. This means that **all** properties are always included in this component. You must keep track yourself which properties are instanced in your shaders and only change those specific properties via this component. Changing non-instanced properties via this component yields undefined behaviour and can even cause performance issues if Unity decides to create new material instances behind the scenes. 11 | 12 | * This component also displays normally-hidden properties. Use caution when modifying these properties. 13 | 14 | * Some more advanced properties cannot be properly represented via this component due to limitations with accessing property info in the currently available Unity APIs. 15 | -------------------------------------------------------------------------------- /Screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jamesdimick/UnityMaterialInstancedPropertiesEditor/7b6e82d50906779890bfb3e4e9a95d9bdc346a99/Screenshot.png --------------------------------------------------------------------------------