赞
踩
unity配置数据可以XML,可以JSON,unity自带的 .asset文件也可以哦,而且能配置的数据类型也比较多。这里说明一下怎么在unity中生成 .asset文件。
首先来个脚本:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum BulletType
{
smallDestroy,
midDestroy,
bigDestroy,
}
//类继承自ScriptableObject
public class BulletTest : ScriptableObject {
public BulletType bulletType = BulletType.midDestroy;
[Range(10,100)]
public int speed = 50;
public int damage = 20;
public GameObject damageEffect;
public Sprite mySprite;
public AudioClip audioClip;
public Texture myTexture;
public Vector3 position;
public List<string> list;
public AudioSource audioSource;
public AutoSail aa;
public MovieTexture moiveTexture;
[System.Serializable]
public class TestClass
{
public string name = "likang";
public int level = 99;
public float attack = 500;
}
public TestClass test;
}
再建一个editor脚本用来生成.asset文件:
using UnityEngine;
using UnityEditor;
using System.IO;
public class AssetTest : Editor {
[MenuItem("CreateAsset/king")]
static void Create()
{
//创建一个ScriptableObject的实例
ScriptableObject bullet = ScriptableObject.CreateInstance<BulletTest>();
if (!bullet)
{
Debug.LogError("bulletTest not found");
return;
}
string path = Application.dataPath + "/BulletFolder";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);//创建指定目录
}
//后缀必须是.asset ; 文件名必须和类名一致
path = string.Format("Assets/BulletFolder/{0}.asset", typeof(BulletTest).ToString());
AssetDatabase.CreateAsset(bullet,path); //在指定路径下创建一个asset配置文件
}
}
生成后的.asset文件如下:
。。。
然后就可以再其他地方用这个.asset配置文件了,注意因为最上面的脚本继承自ScriptableObject,而不是Monobehaviour,所以在unity运行时修改的数据不会在unity停止运行后恢复到原本模样(这个对于微调某些数据来达到最佳效果时还是有用的)。
懒人引用.asset文件:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOne : MonoBehaviour {
public BulletTest bullet;
// Use this for initialization
void Start ()
{
Debug.Log(bullet.damageEffect);
Debug.Log(string.Format("sprite:{0};texture:{1};audioclip:{2}",bullet.mySprite,bullet.myTexture,bullet.audioClip));
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。