TrackBuildREditorInspector.TextureGUI C# (CSharp) Method

TextureGUI() private static method

The Texture display GUI
private static TextureGUI ( TrackBuildRTexture &texture ) : bool
texture TrackBuildRTexture
return bool
    private static bool TextureGUI(ref TrackBuildRTexture texture)
    {
        if(texture.material == null)
            texture.material = new Material(Shader.Find("Diffuse"));

        bool isModified = false;
        string textureName = texture.customName;
        textureName = EditorGUILayout.TextField("Name", textureName);
        if (texture.customName != textureName)
        {
            texture.customName = textureName;
        }

        
        texture.type = (TrackBuildRTexture.Types)EditorGUILayout.EnumPopup("Type", texture.type);

        //Shader Time
        Shader[] tempshaders = (Shader[])Resources.FindObjectsOfTypeAll(typeof(Shader));
        List<string> shaderNames = new List<string>(ShaderProperties.NAMES);
        foreach (Shader shader in tempshaders)
        {
            if (!string.IsNullOrEmpty(shader.name) && !shader.name.StartsWith("__") && !shader.name.Contains("hidden"))
                shaderNames.Add(shader.name);
        }
        int selectedShaderIndex = shaderNames.IndexOf(texture.material.shader.name);
        int newSelectedShaderIndex = EditorGUILayout.Popup("Shader", selectedShaderIndex, shaderNames.ToArray());
        if (selectedShaderIndex != newSelectedShaderIndex)
        {
            texture.material.shader = Shader.Find(shaderNames[newSelectedShaderIndex]);
        }

        texture.physicMaterial = EditorGUILayout.ObjectField("Physics Material", texture.physicMaterial, typeof(PhysicMaterial), false) as PhysicMaterial;

        switch(texture.type)
        {
            case TrackBuildRTexture.Types.Basic:
                Shader selectedShader = texture.material.shader;
                int propertyCount = ShaderUtil.GetPropertyCount(selectedShader);

                for (int s = 0; s < propertyCount; s++)
                {
                    ShaderUtil.ShaderPropertyType propertyTpe = ShaderUtil.GetPropertyType(selectedShader, s);
                    string shaderPropertyName = ShaderUtil.GetPropertyName(selectedShader, s);
                    switch (propertyTpe)
                    {
                        case ShaderUtil.ShaderPropertyType.TexEnv:
                            Texture shaderTexture = texture.material.GetTexture(shaderPropertyName);
                            Texture newShaderTexture = (Texture)EditorGUILayout.ObjectField(shaderPropertyName, shaderTexture, typeof(Texture), false);
                            if (shaderTexture != newShaderTexture)
                            {
                                texture.material.SetTexture(shaderPropertyName, newShaderTexture);
                            }
                            break;

                        case ShaderUtil.ShaderPropertyType.Color:
                            Color shaderColor = texture.material.GetColor(shaderPropertyName);
                            Color newShaderColor = EditorGUILayout.ColorField(shaderPropertyName, shaderColor);
                            if (shaderColor != newShaderColor)
                            {
                                texture.material.SetColor(shaderPropertyName, newShaderColor);
                            }
                            break;

                        case ShaderUtil.ShaderPropertyType.Float:
                            float shaderFloat = texture.material.GetFloat(shaderPropertyName);
                            float newShaderFloat = EditorGUILayout.FloatField(shaderPropertyName, shaderFloat);
                            if (shaderFloat != newShaderFloat)
                            {
                                texture.material.SetFloat(shaderPropertyName, newShaderFloat);
                            }
                            break;

                        case ShaderUtil.ShaderPropertyType.Range:
                            float shaderRange = texture.material.GetFloat(shaderPropertyName);
                            float rangeMin = ShaderUtil.GetRangeLimits(selectedShader, s, 1);
                            float rangeMax = ShaderUtil.GetRangeLimits(selectedShader, s, 2);
                            float newShaderRange = EditorGUILayout.Slider(shaderPropertyName, shaderRange, rangeMin, rangeMax);
                            if (shaderRange != newShaderRange)
                            {
                                texture.material.SetFloat(shaderPropertyName, newShaderRange);
                            }
                            break;

                        case ShaderUtil.ShaderPropertyType.Vector:
                            Vector3 shaderVector = texture.material.GetVector(shaderPropertyName);
                            Vector3 newShaderVector = EditorGUILayout.Vector3Field(shaderPropertyName, shaderVector);
                            if (shaderVector != newShaderVector)
                            {
                                texture.material.SetVector(shaderPropertyName, newShaderVector);
                            }
                            break;
                    }
                }

                if(texture.texture == null)
                    return isModified;

                
            break;

            case TrackBuildRTexture.Types.Substance:

                texture.proceduralMaterial = (ProceduralMaterial)EditorGUILayout.ObjectField("Procedural Material", texture.proceduralMaterial, typeof(ProceduralMaterial), false);

                if (texture.proceduralMaterial != null)
                {
//                    ProceduralMaterial pMat = texture.proceduralMaterial;
//                    GUILayout.Label(pMat.GetGeneratedTexture(pMat.mainTexture.name), GUILayout.Width(400));
                }
                else
                    EditorGUILayout.HelpBox("There is no substance material set.", MessageType.Error);
            break;

            case TrackBuildRTexture.Types.User:
                texture.userMaterial = (Material)EditorGUILayout.ObjectField("User Material", texture.userMaterial, typeof(Material), false);

                if (texture.userMaterial != null)
                {
//                    Material mat = texture.userMaterial;
//                    GUILayout.Label(mat.mainTexture, GUILayout.Width(400));
                }
                else
                    EditorGUILayout.HelpBox("There is no substance material set.", MessageType.Error);
            break;
        }

        bool textureflipped = EditorGUILayout.Toggle("Flip Clockwise", texture.flipped);
        if (textureflipped != texture.flipped)
        {
            isModified = true;
            texture.flipped = textureflipped;
        }

        Vector2 textureUnitSize = texture.textureUnitSize;
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("trackTexture width", GUILayout.Width(175));//, GUILayout.Width(42));
        float textureUnitSizex = EditorGUILayout.FloatField(texture.textureUnitSize.x, GUILayout.Width(25));
        if (textureUnitSizex != textureUnitSize.x)
        {
            isModified = true;
            textureUnitSize.x = textureUnitSizex;
        }
        EditorGUILayout.LabelField("metres", GUILayout.Width(40));//, GUILayout.Width(42));
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("trackTexture height", GUILayout.Width(175));//, GUILayout.Width(42));
        float textureUnitSizey = EditorGUILayout.FloatField(texture.textureUnitSize.y, GUILayout.Width(25));
        if (textureUnitSizey != textureUnitSize.y)
        {
            isModified = true;
            textureUnitSize.y = textureUnitSizey;
        }
        EditorGUILayout.LabelField("metres", GUILayout.Width(40));
        EditorGUILayout.EndHorizontal();
        texture.textureUnitSize = textureUnitSize;

        const int previewTextureUnitSize = 120;
        const int previewPadding = 25;

        EditorGUILayout.LabelField("1 Metre Squared", GUILayout.Width(previewTextureUnitSize));
        GUILayout.Space(previewPadding);
        EditorGUILayout.Space();

        Rect texturePreviewPostion = new Rect(0, 0, 0, 0);
        if (Event.current.type == EventType.Repaint)
            texturePreviewPostion = GUILayoutUtility.GetLastRect();

        Rect previewRect = new Rect(texturePreviewPostion.x, texturePreviewPostion.y, previewTextureUnitSize, previewTextureUnitSize);
        Rect sourceRect = new Rect(0, 0, (1.0f / textureUnitSize.x), (1.0f / textureUnitSize.y));

        GUILayout.Space(previewTextureUnitSize);

        switch(texture.type)
        {
            case TrackBuildRTexture.Types.Basic:
                Graphics.DrawTexture(previewRect, texture.texture, sourceRect, 0, 0, 0, 0);
                break;

#if !UNITY_WEBGL
            case TrackBuildRTexture.Types.Substance:

                if(texture.proceduralMaterial != null)
                {
                    ProceduralMaterial pMat = texture.proceduralMaterial;
//                    GUILayout.Label(pMat.GetGeneratedTexture(pMat.mainTexture.name), GUILayout.Width(400));
                    Graphics.DrawTexture(previewRect, pMat.mainTexture, sourceRect, 0, 0, 0, 0);
                }
                else
                    EditorGUILayout.HelpBox("There is no substance material set.", MessageType.Error);
                break;
#endif

            case TrackBuildRTexture.Types.User:

                if(texture.userMaterial != null)
                {
                    Material mat = texture.userMaterial;
//                    GUILayout.Label(mat.mainTexture, GUILayout.Width(400));
                    Graphics.DrawTexture(previewRect, mat.mainTexture, sourceRect, 0, 0, 0, 0);
                }
                else
                    EditorGUILayout.HelpBox("There is no substance material set.", MessageType.Error);
                break;
        }
        if (isModified)
            GUI.changed = true;

        return isModified;
    }