命令模式:游戏开发设计模式之命令模式(unity3d 示例实现)
对象池模式:游戏开发设计模式之对象池模式(unity3d 示例实现)
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
public GameObject createPerson(GameObject Person)
{
return Instantiate(Person);
}
public GameObject createAnimal(GameObject Animal)
{
return Instantiate(Animal);
}
public GameObject createDragon(GameObject Dragon)
{
return Instantiate(Dragon);
}
}
using UnityEngine;
using System.Collections;
public class Monster : MonoBehaviour {
public string MonsterName;
public int attack;
public int defense;
public string weapon;
// Use this for initialization
/* virtual public Monster clone()
{
return this;
}*/
public GameObject clone()
{
return Instantiate(this.gameObject) as GameObject;
}
}
virtual public Monster clone()
{
return this;
}
public GameObject clone()
{
return Instantiate(this.gameObject) as GameObject;
}
using UnityEngine;
using System.Collections;
public class AnimalMonster : Monster
{
public AnimalMonster()
{
MonsterName = "Animal";
attack = 8;
defense = 15;
weapon = "tooth";
}
}
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
Monster prototype;
// Use this for initialization
public void setPrototype(Monster _prototype)
{
this.prototype = _prototype;
}
public GameObject createMonster()
{
return prototype.clone();
}
}
spawner.setPrototype(People);
spawner.createMonster();
spawner.setPrototype(Animal);
spawner.createMonster();
sing UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
Monster prototype;
// Use this for initialization
public Prototype(Monster _prototype)
{
this.prototype = _prototype;
}
public GameObject createMonster()
{
return prototype.clone();
}
}
Spawner PersonSpawner = new Spawner(People);
Spawner AnimalSpawner = new Spawner(Animal);
PersonSpawner.createMonster();
AnimalSpawner.createMonster();
using UnityEngine;
using System.Collections;
public class SpawnerFor : Spawner
where T : Monster
{
T prototype;
}
Spawner s1;
Spawner s2;
void Start()
{
s1 = new SpawnerFor();
s2 = new SpawnerFor();
s1.setPrototype(People);
s2.setPrototype(Animal);
s1.createMonster();
s2.createMonster();
}
public GameObject createMonster()
{
return new T();
}
{
"MonsterName": "Person",
"attack": 10,
"defense": 10,
"weapon": "Sword"
}
{
"MonsterName": "dwarf saber",
"HP": 10,
"characteristic": "DEF up",
"weapon": "sword",
"attacks": ["hack","chop"]
}
{
"MonsterName": "dwarf archer",
"HP": 10,
"characteristic": "DEF up",
"weapon": "bow",
"attacks": ["shoot","sight"]
}
{
"MonsterName": "dwarf caster",
"HP": 10,
"characteristic": "DEF up",
"weapon": "wand",
"magic": ["fire ball","ice storm"]
}
{
"MonsterName": "dwarf civilian",
"HP": 10,
"characteristic": "DEF up",
}
{
"MonsterName": "dwarf saber",
"prototype": "dwarf civilian",
"weapon": "sword",
"attacks": ["hack","chop"]
}
{
"MonsterName": "dwarf archer",
"prototype": "dwarf civilian",
"weapon": "bow",
"attacks": ["shoot","sight"]
}
{
"MonsterName": "dwarf caster",
"prototype": "dwarf civilian",
"weapon": "wand",
"magic": ["fire ball","ice storm"]
}
{
"Name": "FireBlaze",
"prototype": "long sword",
" additionalDamage ": 10
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#endif
主要类 |
命名空间 |
DataContractJsonSerializer |
System.Runtime.Serialization.Json |
JavaScriptSerializer |
System.Web.Script.Serialization |
JsonArray、JsonObject、JsonValue |
System.Json |
JsonConvert、JArray、JObject、JValue、JProperty |
Newtonsoft.Json |
void WriteJsonToFile(string path, string fileName)
{
System.Text.StringBuilder strB = new System.Text.StringBuilder();
JsonWriter jsWrite = new JsonWriter(strB);
jsWrite.WriteObjectStart();
jsWrite.WritePropertyName("Monster");//Monster为对象数组名
jsWrite.WriteArrayStart();//对象数组
jsWrite.WriteObjectStart();
jsWrite.WritePropertyName("MonsterName");
jsWrite.Write("Person");
jsWrite.WritePropertyName("attack");
jsWrite.Write(10);
jsWrite.WritePropertyName("defense");
jsWrite.Write(10);
jsWrite.WritePropertyName("weapon");
jsWrite.Write("Sword");
jsWrite.WriteObjectEnd();
jsWrite.WriteObjectStart();
jsWrite.WritePropertyName("MonsterName");
jsWrite.Write("Animal");
jsWrite.WritePropertyName("attack");
jsWrite.Write(8);
jsWrite.WritePropertyName("defense");
jsWrite.Write(15);
jsWrite.WritePropertyName("weapon");
jsWrite.Write("tooth");
jsWrite.WriteObjectEnd();
jsWrite.WriteObjectStart();
jsWrite.WritePropertyName("MonsterName");
jsWrite.Write("Dragon");
jsWrite.WritePropertyName("attack");
jsWrite.Write(100);
jsWrite.WritePropertyName("defense");
jsWrite.Write(200);
jsWrite.WritePropertyName("weapon");
jsWrite.Write("fire breath");
jsWrite.WriteObjectEnd();
jsWrite.WriteArrayEnd();
jsWrite.WriteObjectEnd();
Debug.Log(strB);
//创建文件目录
DirectoryInfo dir = new DirectoryInfo(path);
if (dir.Exists)
{
Debug.Log("This file is already exists");
}
else
{
Directory.CreateDirectory(path);
Debug.Log("CreateFile");
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
}
//把json数据写到txt里
StreamWriter sw;
if (File.Exists(fileName))
{
//如果文件存在,那么就向文件继续附加(为了下次写内容不会覆盖上次的内容)
sw = File.AppendText(fileName);
Debug.Log("appendText");
}
else
{
//如果文件不存在则创建文件
sw = File.CreateText(fileName);
Debug.Log("createText");
}
sw.WriteLine(strB);
sw.Close();
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
}
Monster ReadJsonFromTXT(string name)
{
//解析json
Monster monster = new Monster();
JsonData jd = JsonMapper.ToObject(txt.text);
print(jd.IsArray);
JsonData monsterData = jd["Monster"];
print(monsterData.IsArray);
//打印一下数组
for (int i = 0; i < monsterData.Count; i++)
{
if (name == monsterData[i]["MonsterName"].ToString())
{
monster.MonsterName = monsterData[i]["MonsterName"].ToString();
monster.attack = int.Parse(monsterData[i]["attack"].ToString());
monster.defense = int.Parse(monsterData[i]["defense"].ToString());
monster.weapon = monsterData[i]["weapon"].ToString();
}
}
return monster;
}
命令模式:游戏开发设计模式之命令模式(unity3d 示例实现)
对象池模式:游戏开发设计模式之对象池模式(unity3d 示例实现)
博主近期渲染:最近用unity5弄的一些渲染