本篇内容主要讲解“怎么在unity制作配置文件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么在unity制作配置文件”吧!

好直接上代码
using UnityEngine;
using System.Collections;
//由于右键创建配置文件
[CreateAssetMenu()]
public class GlobleData :ScriptableObject {
public bool globle1=false;
public int globle2 = 1;
public GlobleType globleType;
}
public enum GlobleType{
type1,
type2,
type3
}定义几个配置文件的变量,注意这个类必须继承 ScriptableObject。

在代码文件上点击右键/create/globle(类名)

看是不是很厉害,生成了一个带有unitylogo的文件

选中这个文件你可以看到Inspecter显示出了我们刚才定义的参数我们可以自由设置,我们接下来要做的就是读取这些值,我们直接把这个配置文件放到Resources下面然后利用 Resources.Load
using UnityEngine;
using System.Collections;
public class ReadTest : MonoBehaviour {
// Use this for initialization
void Start () {
GlobleData globleData = Resources.Load ("Globle Data");
Debug.Log (globleData.globle1);
Debug.Log (globleData.globle2);
Debug.Log (globleData.globleType);
}
} 是不是非常简单。
锦上添花,接下来我们再写一个EditorWindows的编辑器窗口,用来编辑这个配置文件.
using UnityEngine;
using System.Collections;
using UnityEditor;
[ExecuteInEditMode]
public class EasyWindows :EditorWindow {
private Vector2 _scrollPos = Vector2.zero;
SerializedObject globalData;
[MenuItem("Window/GloblDataEditor")]
static void Init() {
GetWindow(typeof(EasyWindows));
}
void OnEnable()
{
globalData = new SerializedObject (Resources.Load ("Globle Data"));
}
void OnGUI()
{
_scrollPos = GUI.BeginScrollView(
new Rect(0, 0, position.width, position.height),
_scrollPos,
new Rect(0, 0, 310, 310)
);
EditorGUILayout.Space ();
GUILayout.BeginVertical ("Globle", "window",GUILayout.Height(50));
EditorGUILayout.Space ();
EditorGUILayout.PropertyField (globalData.FindProperty ("globle1"));
EditorGUILayout.PropertyField (globalData.FindProperty ("globle2"));
EditorGUILayout.PropertyField (globalData.FindProperty ("globleType"));
GUILayout.EndVertical ();
//一定要添加这个函数,不然修关闭项目后无法保存
//EditorUtility.SetDirty(globalData);
GUI.EndScrollView();
//一定要添加这个函数,不然修关闭项目后数据会丢失
globalData.ApplyModifiedProperties ();
}
} 
大功告成.
这就是简单的配置文件的制作过程,这个一般用于编辑器全局变量的设置,希望大家能用得上,注意这个不能用于做游戏存档,因为打包之后这个文件就无法修改了.
项目地址:https://pan.baidu.com/s/1sl6BwVr
到此,相信大家对“怎么在unity制作配置文件”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。