unity编辑器拓展十二——储存工具栏的数据

大多数时候  我做的unity工具都是一次性的,也就是在场景中操作,关闭unity再打开,或者关闭工具再打开的时候,都没有保存工具上的操作数据;如图:

 

这是在unity里面拖拽两张贴图上去,然后就可以对这两张贴图做各种处理  处理完将贴图保存进unity,但是如果我下次打开的时候,我这两张贴图已经丢失了,又是下面的这种状态,也就是说这些数据没有被保留在工具里。现在想做的就是希望下次打开的时候,上次操作过的两张图还在上面


需要用的知识是:   EditorPrefs.SetString     EditorPrefs.SetFloat    EditorPrefs.SetInt    EditorPrefs.SetBool

EditorPrefs.GetString     EditorPrefs.GetFloat    EditorPrefs.GetInt    EditorPrefs.GetBool

有四种类型可以被保存,比如我们要保存图片,那么我们可以把图片的path以string的方式来保存,当打开工具初始化的时候,按照保存的路径将贴图实例化出来就可以了

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;



public class TextureCombine : EditorWindow
{
    //定义两张贴图
    Texture2D tex1;
    Texture2D tex2;

    //保存数据的path
    //--------------------------------------
    string path01 = "";
    string path02 = "";
    const string save01 = "savepath01";
    const string save02 = "savepath02";
    //--------------------------------------
    [MenuItem("Tools/保存数据")]

    static void window()
    {
        TextureCombine win = (TextureCombine)EditorWindow.GetWindow(typeof(TextureCombine), false, "合并贴图", false);
        win.position = new Rect(200, 200, 250, 200);
        win.Show();
    }
    //打开工具时候就开始初始化 取出保存的路径  然后将贴图根据路径初始化
    //---------------------------------------------
    void OnEnable()
    {
        path01 = EditorPrefs.GetString(save01, path01);
        path02 = EditorPrefs.GetString(save02, path02);
        tex1 = (Texture2D)AssetDatabase.LoadAssetAtPath<Texture2D>(path01);
        tex2 = (Texture2D)AssetDatabase.LoadAssetAtPath<Texture2D>(path02);
    }
    //-------------------------------------------
   

    private void OnGUI()
    {
        GUILayout.BeginVertical();
        {
            GUILayout.BeginHorizontal();
            {
                GUILayout.Space(180);
                EditorGUILayout.LabelField("偏色贴图");
            }
            GUILayout.EndHorizontal();

            tex1 = EditorGUILayout.ObjectField("", tex1, typeof(Texture2D), true) as Texture2D;

            GUILayout.BeginHorizontal();
            {
                GUILayout.Space(180);
                EditorGUILayout.LabelField("高光贴图");
            }
            GUILayout.EndHorizontal();
            tex2 = EditorGUILayout.ObjectField("", tex2, typeof(Texture2D), true) as Texture2D;
   
        }
        GUILayout.EndVertical();


        if (GUI.Button(new Rect(173, 173, 70, 20), "save"))
        {
            if (tex1!=null && tex2 != null)
            {

//取到拖进去的两张图片的路径  并保存
//----------------------------------------------------
                path01 = AssetDatabase.GetAssetPath(tex1);
                path02 = AssetDatabase.GetAssetPath(tex2);
                EditorPrefs.SetString(save01, path01);
                EditorPrefs.SetString(save02, path02);
//-----------------------------------------------------

            }
            else
                this.ShowNotification(new GUIContent("没有贴图"));
        }
    }


    
}





   
	
	




点save的时候  这两张图片就永远被保留在本地机器上

最近在研究一个工具:将一部分数值永久记录下来  然后赋值给每个新的物体,就用了这个方法。这样方便美术

不然还的美术每次开始用工具的时候,先输入一遍数值,以关掉下次又得输入一次 比较麻烦

    

猜你喜欢

转载自blog.csdn.net/baicaishisan/article/details/79507275