├── My Patterns ├── script │ ├── Prototype │ │ ├── LitJson.dll │ │ ├── JSON.meta │ │ ├── Monster.cs.meta │ │ ├── Spawner.cs.meta │ │ ├── test.cs.meta │ │ ├── AnimalMonster.cs.meta │ │ ├── PeoPleMonster.cs.meta │ │ ├── SpawnerFor.cs.meta │ │ ├── JSON │ │ │ ├── JSON_Monster.cs.meta │ │ │ ├── JSON_TEST.cs.meta │ │ │ ├── Spawner_JSON.cs.meta │ │ │ ├── Spawner_JSON.cs │ │ │ ├── JSON_Monster.cs │ │ │ └── JSON_TEST.cs │ │ ├── LitJson.dll.meta │ │ ├── PeoPleMonster.cs │ │ ├── AnimalMonster.cs │ │ ├── SpawnerFor.cs │ │ ├── Monster.cs │ │ ├── Spawner.cs │ │ └── test.cs │ ├── Subclass Sandbox │ │ ├── skill1.cs │ │ ├── skill2.cs │ │ ├── Hero2.cs │ │ └── SkillBase.cs │ └── state │ │ ├── InputEventArgs.cs │ │ ├── State.cs │ │ ├── IdleStateMoveVer.cs │ │ ├── ActState.cs │ │ └── Hero.cs ├── perfab.meta ├── script.meta └── sence 1.meta ├── .gitattributes └── .gitignore /My Patterns/script/Prototype/LitJson.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wolf96/Game-Pattern-Unity3d/HEAD/My Patterns/script/Prototype/LitJson.dll -------------------------------------------------------------------------------- /My Patterns/perfab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9c0744c978528cc499393912aea54219 3 | folderAsset: yes 4 | timeCreated: 1444772905 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /My Patterns/script.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8fc70736135a4ca4d904fb8449812c13 3 | folderAsset: yes 4 | timeCreated: 1444476294 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /My Patterns/sence 1.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 44309631203c7ad4d978524119fb014c 3 | folderAsset: yes 4 | timeCreated: 1444476278 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/JSON.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 08b4a0d2d00810c4db22259ef4d144fd 3 | folderAsset: yes 4 | timeCreated: 1445151573 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/Monster.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 08748a62275534c44ae8a9dc91f58cad 3 | timeCreated: 1445066705 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/Spawner.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 671942183bff21044b0ee404345373a0 3 | timeCreated: 1445066752 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/test.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 97252ba6e632c91478987fb628cf51d4 3 | timeCreated: 1445075322 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/AnimalMonster.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 49c47d6690d51004492029e9515de4cc 3 | timeCreated: 1445066698 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/PeoPleMonster.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: aa0f4e0dd39294b4e8b6d1efbcafed04 3 | timeCreated: 1445066682 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/SpawnerFor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8cacf93d3964c5e40bea52e7ee875ef2 3 | timeCreated: 1445132968 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/JSON/JSON_Monster.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 440618b314d10dc43a268f2dd5fc9396 3 | timeCreated: 1445150013 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/JSON/JSON_TEST.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fb6f638e8e652db4a932d95dfe2cf2a4 3 | timeCreated: 1445146990 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/JSON/Spawner_JSON.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 63adc8f3e84611a409e5a32206e8b1f6 3 | timeCreated: 1445151592 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /My Patterns/script/Subclass Sandbox/skill1.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about Subclass Sandbox pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class skill1 : SkillBase 11 | { 12 | 13 | override public void action() 14 | { 15 | playSound(0); 16 | act(0); 17 | jump(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/LitJson.dll.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9a773ee602b62f142bbc5a1d1d84902f 3 | timeCreated: 1445147360 4 | licenseType: Pro 5 | PluginImporter: 6 | serializedVersion: 1 7 | iconMap: {} 8 | executionOrder: {} 9 | isPreloaded: 0 10 | platformData: 11 | Any: 12 | enabled: 1 13 | settings: {} 14 | Editor: 15 | enabled: 0 16 | settings: 17 | DefaultValueInitialized: true 18 | userData: 19 | assetBundleName: 20 | assetBundleVariant: 21 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/PeoPleMonster.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about prototype pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class PeoPleMonster : Monster 11 | { 12 | public PeoPleMonster() 13 | { 14 | MonsterName = "person"; 15 | attack = 10; 16 | defense = 10; 17 | weapon = "sword"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/AnimalMonster.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about prototype pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class AnimalMonster : Monster 11 | { 12 | public AnimalMonster() 13 | { 14 | MonsterName = "Animal"; 15 | attack = 8; 16 | defense = 15; 17 | weapon = "tooth"; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/SpawnerFor.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about prototype pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class SpawnerFor : Spawner 11 | where T : Monster 12 | // where T : new() 13 | { 14 | T prototype; 15 | // public Monster createMonster() 16 | // { 17 | // return new T(); 18 | // } 19 | } -------------------------------------------------------------------------------- /My Patterns/script/Subclass Sandbox/skill2.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about Subclass Sandbox pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class skill2 : SkillBase 11 | { 12 | 13 | override public void action() 14 | { 15 | playSound(1); 16 | act(1); 17 | // print(heroObject.transform.rotation); 18 | if (heroObject.transform.rotation.w > 0) 19 | move(Vector3.right, 2, true); 20 | else if (heroObject.transform.rotation.w < 0) 21 | move(Vector3.left, 2, true); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/Monster.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about prototype pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | **my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class Monster : MonoBehaviour { 11 | public string MonsterName; 12 | public int attack; 13 | public int defense; 14 | public string weapon; 15 | 16 | 17 | //shallow clone 18 | /* virtual public Monster clone() 19 | { 20 | return this; 21 | }*/ 22 | //deep clone 23 | public GameObject clone(Vector3 pos, Quaternion rot) 24 | { 25 | return Instantiate(this.gameObject, pos, rot) as GameObject; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /My Patterns/script/state/InputEventArgs.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about state pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | using System; 10 | 11 | public class InputEventArgs : EventArgs 12 | { 13 | public /*readonly*/ string input; 14 | public string addition1; 15 | public string addition2; 16 | /* 17 | * act state 1 - 攻击种类(必杀,暴击,第几次攻击) 2 - 武器种类,判断来确定攻击方式 3 - 是否有组合键攻击判断 18 | * move state 19 | */ 20 | public InputEventArgs(string _input, string _addition1, string _addition2) 21 | { 22 | this.input = _input; 23 | this.addition1 = _addition1; 24 | this.addition2 = _addition2; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/Spawner.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about prototype pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class Spawner : MonoBehaviour { 11 | Monster prototype; 12 | // Use this for initialization 13 | public void setPrototype(Monster _prototype) 14 | { 15 | this.prototype = _prototype; 16 | } 17 | 18 | 19 | public GameObject createMonster(Vector3 pos, Quaternion rot) 20 | { 21 | return prototype.clone(pos, rot); 22 | } 23 | 24 | 25 | 26 | /*public GameObject createPerson(GameObject Person) 27 | { 28 | return Instantiate(Person); 29 | } 30 | public GameObject createAnimal(GameObject Animal) 31 | { 32 | return Instantiate(Animal); 33 | } 34 | public GameObject createDragon(GameObject Dragon) 35 | { 36 | return Instantiate(Dragon); 37 | }*/ 38 | } 39 | -------------------------------------------------------------------------------- /My Patterns/script/state/State.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about state pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class State 11 | { 12 | protected static string input; 13 | protected GameObject Person; 14 | protected Hero Person_ctrl; 15 | protected float chargeTime; 16 | protected float MaxTime; 17 | // Use this for initialization 18 | public State(GameObject _Person) 19 | { 20 | this.Person = _Person; 21 | this.Person_ctrl = _Person.GetComponent(); 22 | 23 | } 24 | public virtual void handleInput(object sender, InputEventArgs e) 25 | { 26 | 27 | } 28 | public virtual void UpDate() 29 | { 30 | 31 | } 32 | public virtual void UpDate(string dir,bool isRun) 33 | { 34 | 35 | 36 | } 37 | public virtual void Start() 38 | { 39 | 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /My Patterns/script/Subclass Sandbox/Hero2.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about Subclass Sandbox pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class Hero2 : HeroBase{ 11 | 12 | public AudioClip[] audio=new AudioClip[2]; 13 | 14 | private SkillBase[] skill = new SkillBase[2]; 15 | 16 | 17 | void Start () { 18 | 19 | 20 | skill[0] = new skill1(); 21 | skill[1] = new skill2(); 22 | 23 | skill[0].init(this.GetComponent(),audio,this.GetComponent(),this,this.gameObject); 24 | skill[1].init(this.GetComponent(), audio, this.GetComponent(), this,this.gameObject); 25 | } 26 | 27 | void Update () { 28 | 29 | if (Input.GetKeyDown(KeyCode.Alpha1)) 30 | { 31 | skill[0].action(); 32 | 33 | } 34 | if (Input.GetKeyDown(KeyCode.Alpha2)) 35 | { 36 | skill[1].action(); 37 | 38 | } 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /My Patterns/script/state/IdleStateMoveVer.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about state pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class IdleStateMoveVer : State { 11 | public IdleStateMoveVer(GameObject _Person) 12 | : base(_Person) 13 | { 14 | 15 | } 16 | public override void Start() 17 | { 18 | Person_ctrl.personCtrl.Idle(); 19 | } 20 | public override void handleInput(object sender, InputEventArgs e) 21 | { 22 | input = e.input; 23 | switch (input) 24 | { 25 | case "w"://jump 26 | Person_ctrl.SetMoveState(2); 27 | Person_ctrl.GetNowMoveState().Start(); 28 | 29 | break; 30 | case "a": 31 | 32 | 33 | break; 34 | case "s": 35 | 36 | case "d": 37 | 38 | Person_ctrl.SetMoveState(1); 39 | Person_ctrl.GetNowMoveState().Start(); 40 | break; 41 | } 42 | 43 | } 44 | public override void UpDate() 45 | { 46 | 47 | Person_ctrl.personCtrl.Idle(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /My Patterns/script/Subclass Sandbox/SkillBase.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about Subclass Sandbox pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class SkillBase : MonoBehaviour 11 | { 12 | private AudioSource audioSource; 13 | private AudioClip[] audio; 14 | private Move1 moveFunc; 15 | private Hero2 hero; 16 | protected GameObject heroObject; 17 | 18 | public void init(AudioSource _audioSource, AudioClip[] _audio, Move1 _moveFunc, Hero2 _hero, GameObject _heroObject) 19 | { 20 | this.audio = _audio; 21 | this.audioSource = _audioSource; 22 | this.moveFunc = _moveFunc; 23 | this.hero = _hero; 24 | this.heroObject = _heroObject; 25 | } 26 | protected void act(int actID) 27 | { 28 | hero.Act(); 29 | } 30 | protected void playSound(int soundID) 31 | { 32 | audioSource.PlayOneShot(audio[soundID]); 33 | } 34 | 35 | protected void move(Vector3 dir, float moveSpeed, bool isRun) 36 | { 37 | moveFunc.move(dir, moveSpeed, isRun); 38 | } 39 | protected void jump() 40 | { 41 | moveFunc.jump(); 42 | } 43 | protected void particalEffect() 44 | { 45 | 46 | } 47 | 48 | virtual public void action() 49 | { 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /My Patterns/script/state/ActState.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about state pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class ActState : State 11 | { 12 | 13 | 14 | public ActState(GameObject _Person) 15 | : base(_Person) 16 | { 17 | 18 | } 19 | public override void Start() 20 | { 21 | this.chargeTime = 0.0f; 22 | this.MaxTime = Person_ctrl.personCtrl.getAct1Time(); 23 | 24 | Person_ctrl.personCtrl.Act(); 25 | } 26 | public override void handleInput(object sender, InputEventArgs e) 27 | { 28 | 29 | input = e.input; 30 | switch (input) 31 | { 32 | case "j": 33 | if (chargeTime > MaxTime - 0.1f) 34 | { 35 |  36 | Person_ctrl.GetActState(1).Start(); 37 | 38 | } 39 | break; 40 | case "i": 41 | if (chargeTime > MaxTime - 0.8f) 42 | { 43 | Person_ctrl.SetActState(2); 44 | Person_ctrl.GetNowActState().Start(); 45 | 46 | 47 | } 48 | break; 49 | } 50 |  51 | } 52 | public override void UpDate() 53 | { 54 | 55 | 56 | if (chargeTime < MaxTime) 57 | chargeTime += Time.timeScale / Time.deltaTime; 58 | else 59 | { 60 | this.chargeTime = 0.0f; 61 | Person_ctrl.SetActState(0); 62 | Person_ctrl.GetNowActState().Start(); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/test.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about prototype pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class test : MonoBehaviour { 11 | public Monster People; 12 | public Monster Animal; 13 | Spawner spawner; 14 | 15 | Spawner s1; 16 | Spawner s2; 17 | // Use this for initialization 18 | void Start() 19 | { 20 | spawner = new Spawner(); 21 | if(People!=null) 22 | spawner.setPrototype(People); 23 | 24 | s1 = new SpawnerFor(); 25 | s2 = new SpawnerFor(); 26 | s1.setPrototype(People); 27 | s2.setPrototype(Animal); 28 | } 29 | 30 | // Update is called once per frame 31 | void Update () { 32 | 33 | 34 | if (Input.GetKeyDown(KeyCode.A)) 35 | { 36 | spawner.setPrototype(People); 37 | spawner.createMonster(Vector3.right*Random.Range(-100,100)*0.1f,Quaternion.identity); 38 | 39 | } 40 | if (Input.GetKeyDown(KeyCode.S)) 41 | { 42 | spawner.setPrototype(Animal); 43 | spawner.createMonster(Vector3.right * Random.Range(-100, 100) * 0.1f, Quaternion.identity); 44 | 45 | } 46 | if (Input.GetKeyDown(KeyCode.D)) 47 | { 48 | 49 | s1.createMonster(Vector3.right * Random.Range(-100, 100) * 0.1f, Quaternion.identity); 50 | 51 | 52 | } 53 | if (Input.GetKeyDown(KeyCode.F)) 54 | { 55 | s2.createMonster(Vector3.right * Random.Range(-100, 100) * 0.1f, Quaternion.identity); 56 | 57 | 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/JSON/Spawner_JSON.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about prototype pattern in unity3d 4 | *JSON for Data Modeling 5 | *read JSON from txt 6 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 7 | *my Email: wolf_crixus@sina.cn 8 | */ 9 | 10 | using UnityEngine; 11 | using System.Collections; 12 | using System.Collections.Generic; 13 | using LitJson; 14 | using System.IO; 15 | #if UNITY_EDITOR 16 | using UnityEditor; 17 | #endif 18 | public class Spawner_JSON : MonoBehaviour 19 | { 20 | public TextAsset txt; 21 | string filePath; 22 | string fileName; 23 | // Use this for initialization 24 | void Start() 25 | { 26 | 27 | print(txt.text); 28 | Monster m = createMonster("Person"); 29 | print("name = " + m.MonsterName + " ,attack = " + m.attack); 30 | 31 | } 32 | // Update is called once per frame 33 | Monster ReadJsonFromTXT(string name) 34 | { 35 | //解析json 36 | Monster monster = new Monster(); 37 | JsonData jd = JsonMapper.ToObject(txt.text); 38 | print(jd.IsArray); 39 | JsonData monsterData = jd["Monster"]; 40 | print(monsterData.IsArray); 41 | //打印一下数组 42 | for (int i = 0; i < monsterData.Count; i++) 43 | { 44 | if (name == monsterData[i]["MonsterName"].ToString()) 45 | { 46 | monster.MonsterName = monsterData[i]["MonsterName"].ToString(); 47 | monster.attack = int.Parse(monsterData[i]["attack"].ToString()); 48 | monster.defense = int.Parse(monsterData[i]["defense"].ToString()); 49 | monster.weapon = monsterData[i]["weapon"].ToString(); 50 | } 51 | } 52 | 53 | return monster; 54 | } 55 | public Monster createMonster(string name) 56 | { 57 | return ReadJsonFromTXT(name); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/JSON/JSON_Monster.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about prototype pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | using System.Collections.Generic; 10 | using LitJson; 11 | using System.IO; 12 | #if UNITY_EDITOR 13 | using UnityEditor; 14 | #endif 15 | 16 | public class JSON_Monster : MonoBehaviour 17 | { 18 | public TextAsset txt; 19 | string filePath; 20 | string fileName; 21 | // Use this for initialization 22 | void Start() 23 | { 24 | filePath = Application.dataPath + "/JSON_TXT"; 25 | fileName = filePath + "/Monster.txt"; 26 | 27 | WriteJsonToFile(filePath, fileName); 28 | } 29 | void ReadJsonFromTXT() 30 | { 31 | //解析json 32 | JsonData jd = JsonMapper.ToObject(txt.text); 33 | Debug.Log("hp:" + jd["hp"]); 34 | Debug.Log("mp:" + jd["mp"]); 35 | JsonData weapon = jd["weapon"]; 36 | //打印一下数组 37 | for (int i = 0; i < weapon.Count; i++) 38 | { 39 | Debug.Log("name=" + weapon["name"]); 40 | Debug.Log("color=" + weapon["color"]); 41 | Debug.Log("durability=" + weapon["durability"]); 42 | } 43 | } 44 | void WriteJsonToFile(string path, string fileName) 45 | { 46 | System.Text.StringBuilder strB = new System.Text.StringBuilder(); 47 | JsonWriter jsWrite = new JsonWriter(strB); 48 | jsWrite.WriteObjectStart(); 49 | jsWrite.WritePropertyName("Monster"); 50 | 51 | jsWrite.WriteArrayStart(); 52 | 53 | jsWrite.WriteObjectStart(); 54 | jsWrite.WritePropertyName("MonsterName"); 55 | jsWrite.Write("Person"); 56 | jsWrite.WritePropertyName("attack"); 57 | jsWrite.Write(10); 58 | jsWrite.WritePropertyName("defense"); 59 | jsWrite.Write(10); 60 | jsWrite.WritePropertyName("weapon"); 61 | jsWrite.Write("Sword"); 62 | jsWrite.WriteObjectEnd(); 63 | jsWrite.WriteObjectStart(); 64 | jsWrite.WritePropertyName("MonsterName"); 65 | jsWrite.Write("Animal"); 66 | jsWrite.WritePropertyName("attack"); 67 | jsWrite.Write(8); 68 | jsWrite.WritePropertyName("defense"); 69 | jsWrite.Write(15); 70 | jsWrite.WritePropertyName("weapon"); 71 | jsWrite.Write("tooth"); 72 | jsWrite.WriteObjectEnd(); 73 | jsWrite.WriteObjectStart(); 74 | jsWrite.WritePropertyName("MonsterName"); 75 | jsWrite.Write("Dragon"); 76 | jsWrite.WritePropertyName("attack"); 77 | jsWrite.Write(100); 78 | jsWrite.WritePropertyName("defense"); 79 | jsWrite.Write(200); 80 | jsWrite.WritePropertyName("weapon"); 81 | jsWrite.Write("fire breath"); 82 | jsWrite.WriteObjectEnd(); 83 | 84 | jsWrite.WriteArrayEnd(); 85 | jsWrite.WriteObjectEnd(); 86 | Debug.Log(strB); 87 | //创建文件目录 88 | DirectoryInfo dir = new DirectoryInfo(path); 89 | if (dir.Exists) 90 | { 91 | Debug.Log("This file is already exists"); 92 | } 93 | else 94 | { 95 | Directory.CreateDirectory(path); 96 | Debug.Log("CreateFile"); 97 | #if UNITY_EDITOR 98 | AssetDatabase.Refresh(); 99 | #endif 100 | } 101 | //把json数据写到txt里 102 | StreamWriter sw; 103 | if (File.Exists(fileName)) 104 | { 105 | //如果文件存在,那么就向文件继续附加(为了下次写内容不会覆盖上次的内容) 106 | sw = File.AppendText(fileName); 107 | Debug.Log("appendText"); 108 | } 109 | else 110 | { 111 | //如果文件不存在则创建文件 112 | sw = File.CreateText(fileName); 113 | Debug.Log("createText"); 114 | } 115 | sw.WriteLine(strB); 116 | sw.Close(); 117 | #if UNITY_EDITOR 118 | AssetDatabase.Refresh(); 119 | #endif 120 | 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /My Patterns/script/state/Hero.cs: -------------------------------------------------------------------------------- 1 | /* 2 | *Hi, I'm Lin Dong, 3 | *this script is about state pattern in unity3d 4 | *if you want to get more detail please enter my blog http://blog.csdn.net/wolf96 5 | *my Email: wolf_crixus@sina.cn 6 | */ 7 | using UnityEngine; 8 | using System.Collections; 9 | 10 | public class Hero : MonoBehaviour//hero_state 11 | { 12 | public HeroCtrl personCtrl; 13 | private State heroStateActVer; 14 | private State heroStateMoveVer; 15 | InputEventArgs inputArgs; 16 | private bool isRun = false; 17 | 18 | private State[] hero_act_state = new State[3]; 19 | /* 20 | *0-idle 21 | *1-act 22 | *2-def 23 | */ 24 | private State[] hero_move_state = new State[3]; 25 | /* 26 | *0-idle 27 | *1-move 28 | *2-jump 29 | */ 30 | 31 | // Use this for initialization 32 | public delegate void InputEventHandler(object sender, InputEventArgs e); 33 | public event InputEventHandler InputTran; 34 | // Update is called once per frame 35 | void Input_events(InputEventArgs e) 36 | { 37 | 38 | if (e != null) 39 | { 40 | InputTran(this, e); 41 | } 42 | } 43 | 44 | 45 | void Start() 46 | { 47 | 48 | 49 | personCtrl = this.GetComponent(); 50 | heroStateActVer = new IdleStateActVer(this.gameObject); 51 | heroStateMoveVer = new IdleStateMoveVer(this.gameObject); 52 | inputArgs = new InputEventArgs("", "", ""); 53 | hero_act_state[0] = new IdleStateActVer(this.gameObject); 54 | hero_act_state[1] = new ActState(this.gameObject); 55 | hero_act_state[2] = new DefenseState(this.gameObject); 56 | 57 | hero_move_state[0] = new IdleStateMoveVer(this.gameObject); 58 | hero_move_state[1] = new MoveState(this.gameObject); 59 | hero_move_state[2] = new JumpState(this.gameObject); 60 | 61 | InputTran += new InputEventHandler(heroStateActVer.handleInput); 62 | InputTran += new InputEventHandler(heroStateMoveVer.handleInput); 63 | } 64 | public State GetNowMoveState() 65 | { 66 | // print(heroStateMoveVer.ToString()); 67 | return this.heroStateMoveVer; 68 | } 69 | public State GetMoveState(int stateIndex) 70 | { 71 | return hero_move_state[stateIndex]; 72 | } 73 | public void SetMoveState(int stateIndex) 74 | { 75 | 76 | this.heroStateMoveVer = hero_move_state[stateIndex]; 77 | } 78 | 79 | 80 | 81 | 82 | public State GetNowActState() 83 | { 84 | // print(heroStateActVer.ToString()); 85 | return this.heroStateActVer; 86 | } 87 | public State GetActState(int stateIndex) 88 | { 89 | return hero_act_state[stateIndex]; 90 | } 91 | public void SetActState(int stateIndex) 92 | { 93 | 94 | this.heroStateActVer = hero_act_state[stateIndex]; 95 | } 96 | // Update is called once per frame 97 | void Update() 98 | { 99 | //http://docs.unity3d.com/ScriptReference/Input-inputString.html 100 | #region act&jump input 101 | if (Input.GetKey(KeyCode.LeftShift)) 102 | { 103 | isRun = true; 104 | } 105 | if (Input.anyKeyDown) 106 | {// print(Input.inputString); 107 | foreach (char c in Input.inputString) 108 | { 109 | if (c != null) 110 | { 111 | inputArgs.input = c.ToString();// Input.inputString; 112 | Input_events(inputArgs); 113 | } 114 | } 115 | 116 | } 117 | 118 | #endregion 119 | 120 | #region move input 121 | 122 | 123 | if (Input.GetKey(KeyCode.S)) 124 | { 125 | inputArgs.input = "s"; 126 | inputArgs.input = Input.inputString; 127 | Input_events(inputArgs); 128 | heroStateMoveVer.UpDate("down", isRun); 129 | } 130 | if (Input.GetKey(KeyCode.A)) 131 | { 132 | inputArgs.input = "a"; 133 | inputArgs.input = Input.inputString; 134 | Input_events(inputArgs); 135 | heroStateMoveVer.UpDate("left", isRun); 136 | } 137 | else if (Input.GetKey(KeyCode.D)) 138 | { 139 | inputArgs.input = "d"; 140 | inputArgs.input = Input.inputString; 141 | Input_events(inputArgs); 142 | heroStateMoveVer.UpDate("right", isRun); 143 | } 144 | 145 | isRun = false; 146 | 147 | #endregion 148 | heroStateActVer.UpDate();// 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /My Patterns/script/Prototype/JSON/JSON_TEST.cs: -------------------------------------------------------------------------------- 1 | 2 | using UnityEngine; 3 | using System.Collections; 4 | using System.Collections.Generic; 5 | using LitJson; 6 | using System.IO; 7 | #if UNITY_EDITOR 8 | using UnityEditor; 9 | #endif 10 | 11 | public class JSON_TEST : MonoBehaviour { 12 | public TextAsset txt; 13 | public string filePath; 14 | public string fileName; 15 | // Use this for initialization 16 | void Start () { 17 | filePath = Application.dataPath + "/TextFile"; 18 | fileName = filePath + "/File.txt"; 19 | } 20 | public string GetJsonString() 21 | { 22 | List products = new List(){ 23 | new Monster(){MonsterName="苹果",attack=5}, 24 | new Monster(){MonsterName="橘子",attack=2}, 25 | new Monster(){MonsterName="干柿子",attack=16} 26 | }; 27 | return ""; 28 | // ProductList productlist = new ProductList(); 29 | // productlist.GetProducts = products; 30 | // return new JavaScriptSerializer().Serialize(productlist); 31 | } 32 | 33 | void ReadJson() 34 | { 35 | //注意json格式。我的只能在一行写入啊,要不就报错,懂的大牛,不吝赐教啊,这是为什么呢? 36 | string str = "{'name':'taotao','id':10,'items':[{'itemid':1001,'itemname':'dtao'},{'itemid':1002,'itemname':'test_2'}]}"; 37 | //这里是json解析了 38 | JsonData jd = JsonMapper.ToObject(str); 39 | Debug.Log("name=" + jd["name"]); 40 | Debug.Log("id=" + jd["id"]); 41 | JsonData jdItems = jd["items"]; 42 | //注意这里不能用枚举foreach,否则会报错的,看到网上 43 | //有的朋友用枚举,但是我测试过,会报错,我也不太清楚。 44 | //大家注意一下就好了 45 | for (int i = 0; i < jdItems.Count; i++) 46 | { 47 | Debug.Log("itemid=" + jdItems["itemid"]); 48 | Debug.Log("itemname=" + jdItems["itemname"]); 49 | } 50 | Debug.Log("items is or not array,it's " + jdItems.IsArray); 51 | } 52 | void ReadJsonFromTXT() 53 | { 54 | //解析json 55 | JsonData jd = JsonMapper.ToObject(txt.text); 56 | Debug.Log("hp:" + jd["hp"]); 57 | Debug.Log("mp:" + jd["mp"]); 58 | JsonData weapon = jd["weapon"]; 59 | //打印一下数组 60 | for (int i = 0; i < weapon.Count; i++) 61 | { 62 | Debug.Log("name=" + weapon["name"]); 63 | Debug.Log("color=" + weapon["color"]); 64 | Debug.Log("durability=" + weapon["durability"]); 65 | } 66 | } 67 | 68 | void WriteJsonAndPrint() 69 | { 70 | System.Text.StringBuilder strB = new System.Text.StringBuilder(); 71 | JsonWriter jsWrite = new JsonWriter(strB); 72 | jsWrite.WriteObjectStart(); 73 | jsWrite.WritePropertyName("Name"); 74 | jsWrite.Write("taotao"); 75 | jsWrite.WritePropertyName("Age"); 76 | jsWrite.Write(25); 77 | jsWrite.WritePropertyName("MM"); 78 | jsWrite.WriteArrayStart(); 79 | jsWrite.WriteObjectStart(); 80 | jsWrite.WritePropertyName("name"); 81 | jsWrite.Write("xiaomei"); 82 | jsWrite.WritePropertyName("age"); 83 | jsWrite.Write("17"); 84 | jsWrite.WriteObjectEnd(); 85 | jsWrite.WriteObjectStart(); 86 | jsWrite.WritePropertyName("name"); 87 | jsWrite.Write("xiaoli"); 88 | jsWrite.WritePropertyName("age"); 89 | jsWrite.Write("18"); 90 | jsWrite.WriteObjectEnd(); 91 | jsWrite.WriteArrayEnd(); 92 | jsWrite.WriteObjectEnd(); 93 | Debug.Log(strB); 94 | JsonData jd = JsonMapper.ToObject(strB.ToString()); 95 | Debug.Log("name=" + jd["Name"]); 96 | Debug.Log("age=" + jd["Age"]); 97 | JsonData jdItems = jd["MM"]; 98 | for (int i = 0; i < jdItems.Count; i++) 99 | { 100 | Debug.Log("MM name=" + jdItems["name"]); 101 | Debug.Log("MM age=" + jdItems["age"]); 102 | } 103 | 104 | } 105 | void WriteJsonToFile(string path,string fileName) 106 | { 107 | System.Text.StringBuilder strB = new System.Text.StringBuilder(); 108 | JsonWriter jsWrite = new JsonWriter(strB); 109 | jsWrite.WriteObjectStart(); 110 | jsWrite.WritePropertyName("Name"); 111 | jsWrite.Write("taotao"); 112 | jsWrite.WritePropertyName("Age"); 113 | jsWrite.Write(25); 114 | jsWrite.WritePropertyName("MM"); 115 | jsWrite.WriteArrayStart(); 116 | jsWrite.WriteObjectStart(); 117 | jsWrite.WritePropertyName("name"); 118 | jsWrite.Write("xiaomei"); 119 | jsWrite.WritePropertyName("age"); 120 | jsWrite.Write("17"); 121 | jsWrite.WriteObjectEnd(); 122 | jsWrite.WriteObjectStart(); 123 | jsWrite.WritePropertyName("name"); 124 | jsWrite.Write("xiaoli"); 125 | jsWrite.WritePropertyName("age"); 126 | jsWrite.Write("18"); 127 | jsWrite.WriteObjectEnd(); 128 | jsWrite.WriteArrayEnd(); 129 | jsWrite.WriteObjectEnd(); 130 | Debug.Log(strB); 131 | //创建文件目录 132 | DirectoryInfo dir = new DirectoryInfo(path); 133 | if (dir.Exists) 134 | { 135 | Debug.Log("This file is already exists"); 136 | } 137 | else 138 | { 139 | Directory.CreateDirectory(path); 140 | Debug.Log("CreateFile"); 141 | #if UNITY_EDITOR 142 | AssetDatabase.Refresh(); 143 | #endif 144 | } 145 | //把json数据写到txt里 146 | StreamWriter sw; 147 | if (File.Exists(fileName)) 148 | { 149 | //如果文件存在,那么就向文件继续附加(为了下次写内容不会覆盖上次的内容) 150 | sw = File.AppendText(fileName); 151 | Debug.Log("appendText"); 152 | } 153 | else 154 | { 155 | //如果文件不存在则创建文件 156 | sw = File.CreateText(fileName); 157 | Debug.Log("createText"); 158 | } 159 | sw.WriteLine(strB); 160 | sw.Close(); 161 | #if UNITY_EDITOR 162 | AssetDatabase.Refresh(); 163 | #endif 164 | 165 | } 166 | // Update is called once per frame 167 | void Update () { 168 | 169 | } 170 | } 171 | --------------------------------------------------------------------------------