您好,登录后才能下订单哦!
本篇内容主要讲解“怎么在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<GlobleData> ("Globle Data");获得配置文件。
using UnityEngine;
using System.Collections;
public class ReadTest : MonoBehaviour {
	// Use this for initialization
	void Start () {
	
		GlobleData globleData = Resources.Load<GlobleData> ("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<GlobleData> ("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制作配置文件”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。