Tool
Singleton
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Singleton <T>where T:class,new()
{
private static T instance;
public static T Instance
{
get
{
if(instance == null)
{
instance = new T();
}
return instance;
}
}
}
MessageCenter
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum EventName
{
LoginAccount,
CanLogin,
RegisterAccount,
SaveAccount,
InitRegion,
InitMyBang
}
public class MessageCenter :Singleton<MessageCenter>
{
Dictionary<EventName,Action> myEvents= new Dictionary<EventName,Action>();
public void AddEventListener(EventName type, Action listener)
{
if(myEvents.ContainsKey(type))
{
myEvents[type] += listener;
}
else
{
myEvents.Add(type, listener);
}
}
public void RemoveEventListener(EventName type, Action listener)
{
if(myEvents.ContainsKey(type))
{
myEvents[type]-=listener;
}
}
public void Broadcast(EventName type)
{
if(myEvents.ContainsKey(type))
{
if (myEvents[type] != null)
{
myEvents[type]();
}
}
}
}
public class MessageCenter<T> : Singleton<MessageCenter<T>>
{
Dictionary<EventName, Action<T>> myEvents = new Dictionary<EventName, Action<T>>();
public void AddEventListener(EventName type, Action<T> listener)
{
if (myEvents.ContainsKey(type))
{
myEvents[type] += listener;
}
else
{
myEvents.Add(type, listener);
}
}
public void RemoveEventListener(EventName type, Action<T> listener)
{
if (myEvents.ContainsKey(type))
{
myEvents[type] -= listener;
}
}
public void Broadcast(EventName type,T t)
{
if (myEvents.ContainsKey(type))
{
if (myEvents[type] != null)
{
myEvents[type](t);
}
}
}
}
public class MessageCenter<T,K> : Singleton<MessageCenter<T, K>>
{
Dictionary<EventName, Action<T, K>> myEvents = new Dictionary<EventName, Action<T, K>>();
public void AddEventListener(EventName type, Action<T, K> listener)
{
if (myEvents.ContainsKey(type))
{
myEvents[type] += listener;
}
else
{
myEvents.Add(type, listener);
}
}
public void RemoveEventListener(EventName type, Action<T, K> listener)
{
if (myEvents.ContainsKey(type))
{
myEvents[type] -= listener;
}
}
public void Broadcast(EventName type, T t,K k)
{
if (myEvents.ContainsKey(type))
{
if (myEvents[type] != null)
{
myEvents[type](t,k);
}
}
}
}
FrameWork
ResManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum ResType
{
Prefab,
PrePanel,
FileJson,
Goods,
icon
}
public class ResManager :Singleton<ResManager>
{
Dictionary<string,Object> myRes=new Dictionary<string,Object>();
private T GetLoad<T>(string path) where T : Object
{
Object tempObj;
if(!myRes.TryGetValue(path, out tempObj))
{
tempObj=Resources.Load<T>(path);
myRes.Add(path, tempObj);
}
return tempObj as T;
}
public T GetMyLoad<T>(ResType type,string path) where T : Object
{
return GetLoad<T>(type.ToString()+"/"+path);
}
public void UnLoad()
{
foreach (var item in myRes.Values)
{
Resources.UnloadAsset(item);
System.GC.Collect();
}
}
}
ConfigManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System;
using System.IO;
public class ConfigManager : Singleton<ConfigManager>
{
string ForePath = Application.dataPath + "/Resources/FileJson/";
List<Account> allAccounts = new List<Account>();
public List<Account> AllAccounts
{
get { return allAccounts; }
}
List<Sever> allSever = new List<Sever>();
public List<Sever> AllSever
{
get
{
return allSever;
}
}
List<string> talkMessage= new List<string>();
public List<string> TalkMessage
{
get
{
return talkMessage;
}
}
public void LoadAllData()
{
AddListener();
ParseAccount();
ParseRegion();
ParseTalk();
}
private void ParseTalk()
{
talkMessage = JsonConvert.DeserializeObject<List<string>>(ResManager.Instance.GetMyLoad<TextAsset>(ResType.FileJson, "talk").text);
}
private void AddListener()
{
MessageCenter<List<Account>>.Instance.AddEventListener(EventName.SaveAccount, SaveAccount);
}
private void ParseRegion()
{
allSever = JsonConvert.DeserializeObject<List<Sever>>(ResManager.Instance.GetMyLoad<TextAsset>(ResType.FileJson, "Region").text);
}
private void SaveAccount(List<Account> list)
{
Save("Account", list);
}
private void Save(string path1, List<Account> list)
{
string path= ForePath + path1+".json";
string s=JsonConvert.SerializeObject(list);
File.WriteAllText(path,s);
}
private void ParseAccount()
{
try
{
allAccounts = JsonConvert.DeserializeObject<List<Account>>(ResManager.Instance.GetMyLoad<TextAsset>(ResType.FileJson, "Account").text);
}
catch
{
}
}
}
UiBase
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum PanelType
{
main,
Mutual,
none
}
public class UiBase : MonoBehaviour
{
public PanelType type;
public virtual void Init()
{
}
public virtual void ShowUi()
{
gameObject.SetActive(true);
}
public virtual void HideUi()
{
gameObject.SetActive(false);
}
}
UiManager
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum PanelName
{
LoginPanel,
RegisterPanel,
LoadPanel,
PickRegionPanel,
EtcPanel,
TalkPanel,
BangPanel
}
public class UiManager :Singleton<UiManager>
{
Transform parent;
Dictionary<PanelName,UiBase> myPanels = new Dictionary<PanelName,UiBase>();
Stack<UiBase> allOpenPanel = new Stack<UiBase>();
public UiManager ()
{
parent = GameObject.Find("Canvas").transform;
}
public void OpenUi(PanelName panelName)
{
UiBase tempUi = LoadUi(panelName);
tempUi.ShowUi();
//if(tempUi.type==PanelType.Mutual)
//{
// allOpenPanel.Push(tempUi);
// foreach (var item in allOpenPanel)
// {
// item.HideUi();
// }
// allOpenPanel.Peek().ShowUi();
//}
//else
//{
// tempUi.ShowUi();
//}
}
private UiBase LoadUi(PanelName panelName)
{
UiBase tempUi;
if(!myPanels.TryGetValue(panelName, out tempUi))
{
GameObject go = ResManager.Instance.GetMyLoad<GameObject>(ResType.PrePanel, panelName.ToString());
if(go != null)
{
GameObject obj=GameObject.Instantiate(go,parent);
tempUi=obj.GetComponent<UiBase>();
if(tempUi != null)
{
myPanels.Add(panelName,tempUi);
myPanels[panelName].Init();
}
}
}
return tempUi;
}
public void CloseUi(PanelName panelName)
{
if(myPanels.ContainsKey(panelName))
{
myPanels[panelName].HideUi();
//if (myPanels[panelName].type==PanelType.Mutual)
//{
// allOpenPanel.Pop();
// if(allOpenPanel.Count > 0)
// {
// allOpenPanel.Peek().ShowUi();
// }
//}
}
}
}
ModelBase
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ModelBase
{
public virtual void Init()
{
}
}
ModelManager
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ModelManager :Singleton<ModelManager>
{
Dictionary<Type,ModelBase> myModels= new Dictionary<Type,ModelBase>();
public void LoadAllModel()
{
LoadModel(new LoginModel());
LoadModel(new RegionModel());
LoadModel(new TalkModel());
LoadModel(new BangModel());
}
void LoadModel(ModelBase model)
{
if(!myModels.ContainsKey(model.GetType()))
{
myModels.Add(model.GetType(), model);
myModels[model.GetType()].Init();
}
}
public T GetModel<T>()where T : ModelBase
{
if(myModels.ContainsKey(typeof(T)))
{
return myModels[typeof(T)] as T;
}
else
{
Debug.Log("没有注册模型");
return null;
}
}
}
View
LoginPanel
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LoginPanel : UiBase
{
public InputField accountInput, passwordInput;
public Button loginButton,registerBtn;
public Toggle isRemember;
LoginModel model;
public override void Init()
{
base.Init();
ButtonAddListener();
AddListener();
model=ModelManager.Instance.GetModel<LoginModel>();
//记住密码
Account a=model.GetAccount();
if(a!=null)
{
accountInput.text = a.account;
passwordInput.text = a.password;
}
}
private void AddListener()
{
}
private void ButtonAddListener()
{
loginButton.onClick.AddListener(() =>
{
if(accountInput.text!=null&&passwordInput.text!=null)
{
Account a = new Account();
a.account = accountInput.text;
a.password = passwordInput.text;
MessageCenter<Account>.Instance.Broadcast(EventName.LoginAccount, a);
}
});
registerBtn.onClick.AddListener(() =>
{
UiManager.Instance.CloseUi(PanelName.LoginPanel);
UiManager.Instance.OpenUi(PanelName.RegisterPanel);
});
}
}
RegisterPanel
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RegisterPanel : UiBase
{
public InputField accountInput, passwordInput;
public Button backBtn, registerBtn;
public override void Init()
{
base.Init();
ButtonAddListener();
AddListener();
}
private void AddListener()
{
}
private void ButtonAddListener()
{
backBtn.onClick.AddListener(() =>
{
UiManager.Instance.CloseUi(PanelName.RegisterPanel);
UiManager.Instance.OpenUi(PanelName.LoginPanel);
});
registerBtn.onClick.AddListener(() =>
{
if(accountInput.text!=null&&passwordInput.text!=null)
{
Account a = new Account();
a.account = accountInput.text;
a.password = passwordInput.text;
MessageCenter<Account>.Instance.Broadcast(EventName.RegisterAccount, a);
}
});
}
}
LoadPanel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadPanel : UiBase
{
AsyncOperation m_ao;//场景引用
public Text t;
public Slider slider;
public override void Init()
{
// PlayerPrefs
base.Init();
StartCoroutine(Load());
}
IEnumerator Load()
{
int startpro = 0;//当前数据
int endpro = 100;//总数据
m_ao = SceneManager.LoadSceneAsync("MainScene");//异步加载
m_ao.allowSceneActivation = false;//场景不跳转
while (startpro < endpro)
{
startpro++;
ShowLoginvalue(startpro);
yield return new WaitForSeconds(0.02f);//等待0.02秒执行下面代码
}
if (startpro >= 100)
{
StopCoroutine(Load());//停止协成
m_ao.allowSceneActivation = true;//场景跳转
UiManager.Instance.CloseUi(PanelName.LoadPanel);
}
}
void ShowLoginvalue(int startpro)
{
t.text = startpro + "%";
slider.value = startpro;
}
}
PickRegionPanel
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PickRegionPanel : UiBase
{
public Transform bigParent, littleParent;
RegionModel model;
List<Button> bigRegionButton=new List<Button>();
public override void Init()
{
AddListener();
model=ModelManager.Instance.GetModel<RegionModel>();
model.InitRegion();
base.Init();
}
private void AddListener()
{
MessageCenter<List<Sever>>.Instance.AddEventListener(EventName.InitRegion,InitRegion);
}
private void InitRegion(List<Sever> list)
{
for(int i = 0;i< list.Count; i++)
{
GameObject go = Instantiate(ResManager.Instance.GetMyLoad<GameObject>(ResType.Prefab, "BigRegionItem"), bigParent);
go.GetComponent<BigRegionItem>().Init(list[i]);
bigRegionButton.Add(go.GetComponent<Button>());
}
//添加按钮事件
for(int i = 0;i<bigRegionButton.Count;i++)
{
int j = i;
bigRegionButton[j].onClick.AddListener(() =>
{
SetLittleZone(bigRegionButton[j].gameObject.GetComponent<BigRegionItem>().sever.gameZone);
});
}
}
private void SetLittleZone(List<Zone> gameZone)
{
ClearLittleRegion();
CreatLittleRegion(gameZone);
}
private void CreatLittleRegion(List<Zone> gameZone)
{
for(int i=0;i<gameZone.Count;i++)
{
GameObject little = Instantiate(ResManager.Instance.GetMyLoad<GameObject>(ResType.Prefab, "LittleRegionItem"), littleParent);
little.GetComponent<Toggle>().group=littleParent.GetComponent<ToggleGroup>();
little.GetComponent<LittleRegionItem>().Init(gameZone[i]);
}
}
private void ClearLittleRegion()
{
for(int i = littleParent.childCount-1 ; i>=0;i--)
{
Destroy(littleParent.GetChild(i).gameObject);
}
}
}
TalkPanel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TalkPanel : UiBase
{
public Button nextBtn;
public Transform parent;
TalkModel model;
int index = -1;
public override void Init()
{
base.Init();
model=ModelManager.Instance.GetModel<TalkModel>();
nextBtn.onClick.AddListener(() =>
{
index++;
if(index>=model.talkMessage.Count)
{
for(int i=0;i<parent.childCount;i++)
{
Destroy(parent.GetChild(i).gameObject);
}
index = -1;
UiManager.Instance.CloseUi(PanelName.TalkPanel);
}
else
{
GameObject go = Instantiate(ResManager.Instance.GetMyLoad<GameObject>(ResType.Prefab, "TalkImage"), parent);
go.transform.GetComponentInChildren<Text>().text = model.talkMessage[index];
}
});
}
}
EtcPanel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EtcPanel : UiBase
{
}
BangPanel
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BangPanel : UiBase
{
public InputField nameInput;
public Button creatBtn;
public Text myBangText;
public Transform myBangParent;
BangModel model;
PlayerMove player;
public override void Init()
{
base.Init();
AddListener();
player=GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMove>();
model=ModelManager.Instance.GetModel<BangModel>();
creatBtn.onClick.AddListener(() =>
{
if(nameInput.text!=null)
{
model.CreatBang(player.p,nameInput.text);
nameInput.text = null;
}
});
}
private void AddListener()
{
MessageCenter<BangData>.Instance.AddEventListener(EventName.InitMyBang,InitMyBang);
}
private void InitMyBang(BangData data)
{
ClearParent();
myBangText.text = data.name;
for(int i=0;i<data.member.Count;i++)
{
GameObject go = Instantiate(ResManager.Instance.GetMyLoad<GameObject>(ResType.Prefab, "MemberItem"), myBangParent);
go.GetComponent<MemberItem>().Init(data.member[i]);
}
}
private void ClearParent()
{
for(int i=myBangParent.childCount-1;i>=0;i--)
{
Destroy(myBangParent.GetChild(i).gameObject);
}
}
}
Model
LoginModel
RegionModel
TalkModel
BangModel
Item
BigRegionItem
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BigRegionItem : MonoBehaviour
{
public Text nameText;
public Button severBtn;
public Sever sever;
internal void Init(Sever sever1)
{
sever=sever1;
nameText.text = sever.serverName;
severBtn.onClick.AddListener(() =>
{
});
}
}
LittleRegionItem
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class LittleRegionItem : MonoBehaviour,IPointerClickHandler
{
Zone zone;
public Text nameText;
float lastTime;
RegionModel model;
internal void Init(Zone zone1)
{
model=ModelManager.Instance.GetModel<RegionModel>();
zone = zone1;
nameText.text = zone.zoneName;
}
public void OnPointerClick(PointerEventData eventData)
{
//if(Time.realtimeSinceStartup-lastTime<0.3f)
//{
// model.SetZone(zone);
// UiManager.Instance.CloseUi(PanelName.PickRegionPanel);
// UiManager.Instance.OpenUi(PanelName.LoadPanel);
//}
//lastTime = Time.realtimeSinceStartup;
if (eventData.clickCount==2)
{
model.SetZone(zone);
UiManager.Instance.CloseUi(PanelName.PickRegionPanel);
UiManager.Instance.OpenUi(PanelName.LoadPanel);
}
}
}
ServiceNum
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ServiceNum : MonoBehaviour
{
Transform parent;
GameObject service;
Text numText;
RegionModel model;
// Start is called before the first frame update
void Start()
{
model=ModelManager.Instance.GetModel<RegionModel>();
parent = GameObject.Find("Canvas").transform;
service=Instantiate(ResManager.Instance.GetMyLoad<GameObject>(ResType.Prefab, "ServiceNum"),parent);
numText=service.GetComponentInChildren<Text>();
numText.text = model.GetPickService();
}
// Update is called once per frame
void Update()
{
service.transform.position=Camera.main.WorldToScreenPoint(transform.position+Vector3.up);
}
}
MemberItem
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MemberItem : MonoBehaviour
{
public Text nameText, levelText, typeText;
internal void Init(PlayerData playerData)
{
nameText.text= playerData.name;
levelText.text=playerData.level.ToString();
typeText.text= playerData.type.ToString();
}
}
PlayerMove
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
Etc etc;
public PlayerData p;
// Start is called before the first frame update
void Start()
{
etc=GameObject.FindGameObjectWithTag("Etc").GetComponent<Etc>();
}
internal void Init(PlayerData playerData)
{
p = playerData;
}
// Update is called once per frame
void Update()
{
Move();
}
private void Move()
{
float H = etc.GetAxis("Horizontal");
float V = etc.GetAxis("Vertical");
Vector3 pos = new Vector3(V, 0, H);
if (pos != Vector3.zero)
{
transform.Translate(Vector3.forward * 10 * Time.deltaTime * V);
transform.Rotate(Vector3.up * 100 * Time.deltaTime * H);
}
}
}
Npc
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Npc : MonoBehaviour
{
private void OnMouseDown()
{
// UiManager.Instance.OpenUi(PanelName.TalkPanel);
UiManager.Instance.OpenUi(PanelName.BangPanel);
}
}
Etc
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Etc : MonoBehaviour,IBeginDragHandler, IEndDragHandler,IDragHandler
{
private static Etc instance;
public static Etc Instance
{
get { return instance; }
}
Vector3 orStart, dir;
float dis;
float R = 100;
RectTransform rect;
private void Start()
{
rect=transform as RectTransform;
orStart = transform.position;
}
public void OnBeginDrag(PointerEventData eventData)
{
}
public void OnDrag(PointerEventData eventData)
{
dis=Vector2.Distance(Input.mousePosition, orStart);
if(dis<R)
{
transform.position = Input.mousePosition;
}
else
{
dir=Input.mousePosition-orStart;
transform.position = orStart+dir.normalized*R;
}
}
public void OnEndDrag(PointerEventData eventData)
{
transform.position = orStart;
}
public float GetAxis(string name)
{
if(name=="Horizontal")
{
return rect.anchoredPosition.x/R;
}
if(name=="Vertical")
{
return rect.anchoredPosition.y/R;
}
return 0f;
}
}
MainCamera
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainCamera : MonoBehaviour
{
GameObject player;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update()
{
transform.position= player.transform.position+player.transform.forward*-3+Vector3.up*3;
transform.eulerAngles = player.transform.eulerAngles+Vector3.right*30;
}
}