一、应用
把策划数值转为游戏数值
二、序列化类及数据结构类
public class HeroAssetObject: ScriptableObject{
public Hero[] heroList;
}注意前面的序列化标识:[System.Serializable]
[System.Serializable]
public class Hero{
public int id;
public int hp;
public int ap;
public int mp;
public string name;
public int age;
}
三、编辑器下创建序列化文件
public class CreateAssetFile{
[MenuItem("Age/CreateHeroAsset")]
public static void CreateHeroAsset()
{
if(!Directory.Exists(Application.streamingAssetsPath))
{
Directory.CreateDirectory(Application.streamingAssetsPath);
}
HeroAssetObject obAsset=new HeroAssetObject();
obAsset.heroList=readHeroAssetData();
AssetDatabase.CreateAsset(obAsset,"Assets/StreamingAssets/a.asset");
BuildPipeline.BuildAssetBundle(obAsset,null,Application.streamingAssetsPath+"/a.assetbundle"
,BuildAssetBundleOptions.CollectDependencies,EditorUserBuildSettings.activeBuildTarget);
AssetDatabase.Refresh();
}
public static Hero[] readHeroAssetData()
{
Hero[] heroList=null;
string[] heroAssData= File.ReadAllLines(Application.dataPath+"/HeroAssets.csv");
if(heroAssData!=null)
{
heroList=new Hero[heroAssData.Length-1];
for(int i=1;i<heroAssData.Length;i++)
{
heroList[i-1]=new Hero();
string[] infors=heroAssData[i].Split(',');
heroList[i-1].id=int.Parse(infors[0]);
heroList[i-1].hp=int.Parse(infors[1]);
heroList[i-1].ap=int.Parse(infors[2]);
heroList[i-1].mp=int.Parse(infors[3]);
heroList[i-1].age=int.Parse(infors[4]);
Debug.Log("I:"+i+" heroAssData:"+heroAssData[i]);
}
}
return heroList;
}
}
四、游戏中动态加载资源
public class Main : MonoBehaviour {
public HeroAssetObject assObject;
// Use this for initialization
void Start () {
StartCoroutine(loadHeroAsset());
}
IEnumerator loadHeroAsset()
{
WWW www=new WWW("file://"+Application.streamingAssetsPath+"/a.assetbundle");
yield return www;
if(www.error==null)
{
AssetBundle ab=www.assetBundle;
HeroAssetObject heroAssOb=(HeroAssetObject)ab.mainAsset;
assObject=heroAssOb;
ab.Unload(false);
ab=null;
}
else
{
Debug.Log("error:"+www.error);
}
www.Dispose();
www=null;
}
}
本文介绍了一个Unity游戏资源管理系统,包括序列化英雄角色数据并创建资产文件的方法,以及在游戏中动态加载这些资源的过程。
2605

被折叠的 条评论
为什么被折叠?



