Пример #1
0
	private void GenerateAndSaveTexture(string path)
	{
		if(string.IsNullOrEmpty(path))
			return;

		TCP2_GradientManager.SaveGradientTexture(mGradient, textureWidth, path);
	}
Пример #2
0
	void InitEditMode(Texture2D texture, Material[] materials)
	{
		textureEdited = false;
		editMode = true;
		linkedTexture = texture;
		linkedImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture));
		linkedMaterials = materials;
		TCP2_GradientManager.SetGradientFromUserData(linkedImporter.userData, mGradient);
		UpdateGradientPreview();
	}
Пример #3
0
	private void SaveEditedTexture()
	{
		if(textureEdited)
		{
			//Save data to file
			File.WriteAllBytes(Application.dataPath + AssetDatabase.GetAssetPath(linkedTexture).Substring(6), linkedTexture.EncodeToPNG());

			//Update linked texture userData
			linkedImporter.userData = TCP2_GradientManager.GradientToUserData(mGradient);
		}
		textureEdited = false;
	}
Пример #4
0
        private void GenerateAndSaveTexture(string path, bool is2dRamp)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (is2dRamp)
            {
                TCP2_GradientManager.SaveGradientTexture2D(m2dGradients, textureWidth, textureHeight, path);
            }
            else
            {
                TCP2_GradientManager.SaveGradientTexture(mGradient, textureWidth, path);
            }
        }
Пример #5
0
        void InitEditMode(Texture2D texture, Material[] materials)
        {
            textureIsDirty  = false;
            editMode        = true;
            linkedTexture   = texture;
            linkedImporter  = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture));
            linkedMaterials = materials;
            var gradientList = TCP2_GradientManager.GetGradientsFromUserData(linkedImporter.userData);

            if (gradientList.Count == 1)
            {
                mGradient         = gradientList[0];
                editedTextureIs2d = false;
            }
            else
            {
                m2dGradients      = gradientList.ToArray();
                editedTextureIs2d = true;
            }

            UpdateGradientPreview();
        }
Пример #6
0
    public override void OnGUI(Rect position, MaterialProperty prop, string label, MaterialEditor editor)
    {
        //Find default ramp texture if needed
        if (prop.textureValue == null)
        {
            if (DefaultRampTexture == null && !DefaultTextureSearched)
            {
                DefaultRampTexture     = TryFindDefaultRampTexture();
                DefaultTextureSearched = true;
            }

            if (DefaultRampTexture != null)
            {
                prop.textureValue = DefaultRampTexture;
            }
        }

        float indent = EditorGUI.indentLevel * 16;

        //Label
        var labelRect = position;

        labelRect.height = 16f;
        float space = labelRect.height + 4;

        position.y      += space - 3;
        position.height -= space;
        EditorGUI.PrefixLabel(labelRect, new GUIContent(label));

        //Texture object field
        position.height = 16f;
        var newTexture = (Texture)EditorGUI.ObjectField(position, prop.textureValue, typeof(Texture2D), false);

        if (newTexture != prop.textureValue)
        {
            prop.textureValue = newTexture;
            assetImporter     = null;
        }

        //Preview texture override (larger preview, hides texture name)
        var previewRect = new Rect(position.x + indent - 1, position.y + 1, position.width - indent - 18, position.height - 2);

        if (prop.hasMixedValue)
        {
            var col = GUI.color;
            GUI.color = EditorGUIUtility.isProSkin ? new Color(.25f, .25f, .25f) : new Color(.85f, .85f, .85f);
            EditorGUI.DrawPreviewTexture(previewRect, Texture2D.whiteTexture);
            GUI.color = col;
            GUI.Label(previewRect, "―");
        }
        else if (prop.textureValue != null)
        {
            EditorGUI.DrawPreviewTexture(previewRect, prop.textureValue);
        }

        if (prop.textureValue != null)
        {
            assetImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(prop.textureValue));
        }

        //Edit button
        var buttonRect = labelRect;

        buttonRect.width = 70;
        buttonRect.x     = labelRect.x + (labelRect.width - 150);
        if (GUI.Button(buttonRect, "Create New", EditorStyles.miniButtonLeft))
        {
            string path = EditorUtility.SaveFilePanel("Create New Ramp Texture", TCP2_GradientManager.LAST_SAVE_PATH, "TCP2_CustomRamp", "png");
            if (!string.IsNullOrEmpty(path))
            {
                TCP2_GradientManager.LAST_SAVE_PATH = System.IO.Path.GetDirectoryName(path);

                //Create texture and save PNG
                var projectPath = path.Replace(Application.dataPath, "Assets");
                TCP2_GradientManager.CreateAndSaveNewGradientTexture(256, projectPath);

                //Load created texture
                var texture = AssetDatabase.LoadAssetAtPath <Texture2D>(projectPath);
                assetImporter = AssetImporter.GetAtPath(projectPath);

                //Assign to material(s)
                prop.textureValue = texture;

                //Open for editing
                TCP2_RampGenerator.OpenForEditing(texture, editor.targets, true);
            }
        }
        buttonRect.x    += buttonRect.width;
        buttonRect.width = 80;
        bool enabled = GUI.enabled;

        GUI.enabled = (assetImporter != null) && assetImporter.userData.StartsWith("GRADIENT") && !prop.hasMixedValue;
        if (GUI.Button(buttonRect, GUI.enabled ? editButtonLabel : editButtonDisabledLabel, EditorStyles.miniButtonRight))
        {
            TCP2_RampGenerator.OpenForEditing((Texture2D)prop.textureValue, editor.targets, true);
        }
        GUI.enabled = enabled;
    }
Пример #7
0
	void OnGUI()
	{
		EditorGUILayout.BeginHorizontal();
		TCP2_GUI.HeaderBig(editMode ? "TCP 2 - RAMP EDITOR" : "TCP 2 - RAMP GENERATOR");
		TCP2_GUI.HelpButton("Ramp Generator");
		EditorGUILayout.EndHorizontal();
		TCP2_GUI.Separator();

		if(editMode)
		{
			var msg = "This will affect <b>all materials</b> that use this texture!" +
				(editModeFromMaterial ? "\n\nSave as a new texture first if you want to affect this material only." : "\n\nSave as a new texture if you want to keep the original ramp.");
			EditorGUILayout.LabelField(GUIContent.none, new GUIContent(msg, TCP2_GUI.GetHelpBoxIcon(MessageType.Warning)), TCP2_GUI.HelpBoxRichTextStyle);

			var rect = EditorGUILayout.GetControlRect(GUILayout.Height(16f));
			var lw = EditorGUIUtility.labelWidth;
			EditorGUIUtility.labelWidth = 50f;
			var enabled = GUI.enabled;
			GUI.enabled = false;
			EditorGUI.ObjectField(rect, "Editing: ", linkedTexture, typeof(Texture2D), false);
			EditorGUIUtility.labelWidth = lw;
			GUI.enabled = enabled;
		}

		GUILayout.Label("Click on the gradient to edit it:");
		var so = new SerializedObject(this);
		var sp = so.FindProperty("mGradient");
		EditorGUILayout.PropertyField(sp, GUIContent.none);

		if(!editMode)
		{
			textureWidth = EditorGUILayout.IntField("TEXTURE SIZE:", textureWidth);
			EditorGUILayout.BeginHorizontal();
			if(GUILayout.Button("64", EditorStyles.miniButtonLeft)) textureWidth = 64;
			if(GUILayout.Button("128", EditorStyles.miniButtonMid)) textureWidth = 128;
			if(GUILayout.Button("256", EditorStyles.miniButtonMid)) textureWidth = 256;
			if(GUILayout.Button("512", EditorStyles.miniButtonMid)) textureWidth = 512;
			if(GUILayout.Button("1024", EditorStyles.miniButtonRight)) textureWidth = 1024;
			EditorGUILayout.EndHorizontal();
		}

		if (GUI.changed)
		{
			so.ApplyModifiedProperties();
			mGradient.alphaKeys = new[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(1f, 1f) };

			if(editMode)
			{
				textureEdited = true;

				//Update linked texture
				var pixels = TCP2_GradientManager.GetPixelsFromGradient(mGradient, linkedTexture.width);
				linkedTexture.SetPixels(pixels);
				linkedTexture.Apply(true, false);
			}
		}

		GUILayout.Space(8f);
		EditorGUILayout.BeginHorizontal();
		GUILayout.FlexibleSpace();
		if(editMode)
		{
			if(GUILayout.Button("Discard", GUILayout.Width(90f), GUILayout.Height(20f)))
			{
				DiscardEditedTexture();
				if(editModeFromMaterial)
					Close();
				else
					OpenTool();
			}
			if(GUILayout.Button("Apply", GUILayout.Width(90f), GUILayout.Height(20f)))
			{
				SaveEditedTexture();
				if(editModeFromMaterial)
					Close();
				else
					OpenTool();
			}
			EditorGUILayout.EndHorizontal();
			EditorGUILayout.BeginHorizontal();
			GUILayout.FlexibleSpace();
		}

		var saveButton = false;
		if(editMode)
			saveButton = GUILayout.Button("Save as...", EditorStyles.miniButton, GUILayout.Width(120f), GUILayout.Height(16f));
		else
			saveButton = GUILayout.Button("GENERATE", GUILayout.Width(120f), GUILayout.Height(34f));
		if(saveButton)
		{
			var path = EditorUtility.SaveFilePanel("Save Generated Ramp", TCP2_GradientManager.LAST_SAVE_PATH, editMode ? linkedTexture.name : "TCP2_CustomRamp", "png");
			if(!string.IsNullOrEmpty(path))
			{
				TCP2_GradientManager.LAST_SAVE_PATH = Path.GetDirectoryName(path);
				var projectPath = path.Replace(Application.dataPath, "Assets");
				GenerateAndSaveTexture(projectPath);

				if(editMode)
				{
					var newtexture = AssetDatabase.LoadAssetAtPath<Texture2D>(projectPath);
					if(newtexture != null)
					{
						foreach(var mat in linkedMaterials)
						{
							mat.SetTexture("_Ramp", newtexture);
							EditorUtility.SetDirty(mat);
						}
					}

					//Reinitialize edit mode
					InitEditMode(newtexture, linkedMaterials);
				}
			}
		}
		EditorGUILayout.EndHorizontal();

		if(!editMode)
		{
			EditorGUILayout.BeginHorizontal();
			GUILayout.FlexibleSpace();
			if(GUILayout.Button("Load Texture", EditorStyles.miniButton, GUILayout.Width(120f)))
			{
				LoadTexture();
			}
			EditorGUILayout.EndHorizontal();
		}
	}
Пример #8
0
        void OnGUI()
        {
            EditorGUILayout.BeginHorizontal();
            TCP2_GUI.HeaderBig(editMode ? "TCP 2 - RAMP EDITOR" : "TCP 2 - RAMP GENERATOR");
            TCP2_GUI.HelpButton("Ramp Generator");
            EditorGUILayout.EndHorizontal();
            TCP2_GUI.Separator();

            serializedObject.Update();

            if (editMode)
            {
                if (!isNewTexture)
                {
                    var msg = "This will affect <b>all materials</b> that use this texture!" +
                              (editModeFromMaterial ? "\n\nSave as a new texture first if you want to affect this material only." : "\n\nSave as a new texture if you want to keep the original ramp.");
                    EditorGUILayout.LabelField(GUIContent.none, new GUIContent(msg, TCP2_GUI.GetHelpBoxIcon(MessageType.Warning)), TCP2_GUI.HelpBoxRichTextStyle);
                }

                var rect = EditorGUILayout.GetControlRect(GUILayout.Height(16f));
                var lw   = EditorGUIUtility.labelWidth;
                EditorGUIUtility.labelWidth = 50f;
                var enabled = GUI.enabled;
                GUI.enabled = false;
                EditorGUI.ObjectField(rect, "Editing: ", linkedTexture, typeof(Texture2D), false);
                EditorGUIUtility.labelWidth = lw;
                GUI.enabled = enabled;
            }
            else
            {
                /*
                 * EditorGUILayout.BeginHorizontal();
                 * if (GUILayout.Toggle(tabIndex == 0, "1D RAMP", TCP2_GUI.Tab))
                 *      tabIndex = 0;
                 * if (GUILayout.Toggle(tabIndex == 1, "2D RAMP", TCP2_GUI.Tab))
                 *      tabIndex = 1;
                 * GUILayout.FlexibleSpace();
                 * EditorGUILayout.EndHorizontal();
                 * TCP2_GUI.SeparatorSimple();
                 */
            }

            if (isRamp1d)
            {
                GUILayout.Label("Click on the gradient to edit it:");
                EditorGUILayout.PropertyField(gradientProperty, GUIContent.none);
            }
            else
            {
                scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
                gradients2dList.DoLayoutList();
                EditorGUILayout.EndScrollView();
            }

            if (!editMode)
            {
                if (isRamp1d)
                {
                    textureWidth = EditorGUILayout.IntField("TEXTURE SIZE:", textureWidth);
                    EditorGUILayout.BeginHorizontal();
                    if (GUILayout.Button("64", EditorStyles.miniButtonLeft))
                    {
                        textureWidth = 64;
                    }
                    if (GUILayout.Button("128", EditorStyles.miniButtonMid))
                    {
                        textureWidth = 128;
                    }
                    if (GUILayout.Button("256", EditorStyles.miniButtonMid))
                    {
                        textureWidth = 256;
                    }
                    if (GUILayout.Button("512", EditorStyles.miniButtonMid))
                    {
                        textureWidth = 512;
                    }
                    if (GUILayout.Button("1024", EditorStyles.miniButtonRight))
                    {
                        textureWidth = 1024;
                    }
                    EditorGUILayout.EndHorizontal();
                }
                else if (isRamp2d)
                {
                    GUILayout.BeginHorizontal();
                    textureWidth = EditorGUILayout.IntField("TEXTURE SIZE:", textureWidth);
                    GUILayout.Label("x");
                    textureHeight = EditorGUILayout.IntField(GUIContent.none, textureHeight);
                    GUILayout.EndHorizontal();

                    EditorGUILayout.BeginHorizontal();
                    if (GUILayout.Button("64", EditorStyles.miniButtonLeft))
                    {
                        textureWidth = 64;
                    }
                    if (GUILayout.Button("128", EditorStyles.miniButtonMid))
                    {
                        textureWidth = 128;
                    }
                    if (GUILayout.Button("256", EditorStyles.miniButtonMid))
                    {
                        textureWidth = 256;
                    }
                    if (GUILayout.Button("512", EditorStyles.miniButtonMid))
                    {
                        textureWidth = 512;
                    }
                    if (GUILayout.Button("1024", EditorStyles.miniButtonRight))
                    {
                        textureWidth = 1024;
                    }
                    GUILayout.Space(8);
                    if (GUILayout.Button("64", EditorStyles.miniButtonLeft))
                    {
                        textureHeight = 64;
                    }
                    if (GUILayout.Button("128", EditorStyles.miniButtonMid))
                    {
                        textureHeight = 128;
                    }
                    if (GUILayout.Button("256", EditorStyles.miniButtonMid))
                    {
                        textureHeight = 256;
                    }
                    if (GUILayout.Button("512", EditorStyles.miniButtonMid))
                    {
                        textureHeight = 512;
                    }
                    if (GUILayout.Button("1024", EditorStyles.miniButtonRight))
                    {
                        textureHeight = 1024;
                    }
                    EditorGUILayout.EndHorizontal();
                }
            }

            if (GUI.changed)
            {
                serializedObject.ApplyModifiedProperties();

                mGradient.alphaKeys = new[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(1f, 1f) };

                if (editMode)
                {
                    textureIsDirty = true;

                    //Update linked texture
                    if (editedTextureIs2d)
                    {
                        TCP2_GradientManager.SetPixelsFromGradients(linkedTexture, m2dGradients, linkedTexture.width, linkedTexture.height);
                    }
                    else
                    {
                        var pixels = TCP2_GradientManager.GetPixelsFromGradient(mGradient, linkedTexture.width, linkedTexture.height);
                        linkedTexture.SetPixels(pixels);
                        linkedTexture.Apply(true, false);
                    }
                }
            }

            GUILayout.Space(8f);
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (editMode)
            {
                if (GUILayout.Button("Discard", GUILayout.Width(90f), GUILayout.Height(20f)))
                {
                    DiscardEditedTexture();
                    if (editModeFromMaterial)
                    {
                        Close();
                    }
                    else
                    {
                        OpenTool();
                    }
                }
                if (GUILayout.Button("Apply", GUILayout.Width(90f), GUILayout.Height(20f)))
                {
                    SaveEditedTexture();
                    if (editModeFromMaterial)
                    {
                        Close();
                    }
                    else
                    {
                        OpenTool();
                    }
                }
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
            }

            var saveButton = false;

            EditorGUI.BeginDisabledGroup(isRamp2d && (m2dGradients == null || m2dGradients.Length < 2));
            if (editMode)
            {
                saveButton = GUILayout.Button("Save as...", EditorStyles.miniButton, GUILayout.Width(120f), GUILayout.Height(16f));
            }
            else
            {
                saveButton = GUILayout.Button("GENERATE", GUILayout.Width(120f), GUILayout.Height(34f));
            }
            EditorGUI.EndDisabledGroup();
            if (saveButton)
            {
                var path = EditorUtility.SaveFilePanel("Save Generated Ramp", TCP2_GradientManager.LAST_SAVE_PATH, editMode ? linkedTexture.name : "TCP2_CustomRamp", "png");
                if (!string.IsNullOrEmpty(path))
                {
                    TCP2_GradientManager.LAST_SAVE_PATH = Path.GetDirectoryName(path);
                    var projectPath = path.Replace(Application.dataPath, "Assets");
                    GenerateAndSaveTexture(projectPath, isRamp2d);

                    if (editMode)
                    {
                        var newTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(projectPath);
                        if (newTexture != null)
                        {
                            foreach (var mat in linkedMaterials)
                            {
                                mat.SetTexture("_Ramp", newTexture);
                                EditorUtility.SetDirty(mat);
                            }
                        }

                        //Reinitialize edit mode
                        InitEditMode(newTexture, linkedMaterials);
                    }
                }
            }
            EditorGUILayout.EndHorizontal();

            if (!editMode)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Load Texture", EditorStyles.miniButton, GUILayout.Width(120f)))
                {
                    LoadTexture();
                }
                EditorGUILayout.EndHorizontal();
            }
        }