续上
2.18、Message
++++Message:消息。
++++Scripts/YanlzFramework/Common/Message/Message.cs
++2.18.1、Message.cs(消息)
//消息:(YanlzFramework/Common/Message/Message.cs)
using System;
using System.Collections;
using System.Collections.Generic;
namespace YanlzFramework{
//消息描述:
//foreach();
//message[key] = value
//Remove()
//Send()
//sender
//Type or Name
//Content
public class Message : IEnumerable<KeyValuePair<string, object>>{
private Dictionary<string, object> dicDatas = null;
public string Name{ get; private set; }
public object Sender{ get; private set; }
public object Content{ get; set; }
#region message[key] = value or data = message[key]
//实现类似索引器方法:message[key] = value功能
public object this[string key]{
get{
if(null == dicDatas || dicDatas.ContainsKey(key)){
return;
}
return dicDatas[key];
}
set{
if(null == dicDatas){
dicDatas = new Dictionary<string, object>();
}
if(dicDatas.ContainsKey(key)){
dicDatas[key] = value;
}else{
dicDatas.Add(key, value);
}
}
}
#endregion
#region IEnumerable implementation
public IEnumerator<KeyValuePair<string, object>> GetEnumerator(){
if(null == dicDatas){
yield break;
}
foreach(KeyValuePair<string, object> kvp in dicDatas){
yield return kvp;
}
}
#endregion
#region IEnumerable implementation
IEnumerator IEnumerable.GetEnumerator(){
return dicDatas.GetEnumerator(); //借用Dictionary接口
}
#endregion
#region Message Construction Function(构造函数)
public Message(string name, object sender){
Name = name;
Sender = sender;
Content = null;
}
public Message(string name, object sender, object content){
Name = name;
Sender = sender;
Content = content;
}
public Message(string name, object sender, object content, params object[] _dicParams){
Name = name;
Sender = sender;
Content = content;
if(_dicParams.GetType() == typeof(Dictionary<string, object>)){
foreach(object _dicParam in _dicParams){
foreach(KeyValuePair<string, object> kvp in _dicParam as Dictionary<string, object>){
//dicDatas[kvp.Key] = kvp.Value; //error
this[kvp.Key] = kvp.Value; //利用索引器
}
}
}
}
//类似C++拷贝构造函数
public Message(Message message){
Name = message.Name;
Sender = message.Sender;
Content = message.Content;
foreach(KeyValuePair<string, object> kvp in message.dicDatas){
this[kvp.Key] = kvp.Value;
}
}
#endregion
#region Add & Remove
public void Add(string key, object value){
this[key] = value;
}
public void Remove(string key){
if(null != dicDatas && dicDatas.ContainsKey(key)){
dicDatas.Remove(key);
}
}
#endregion
#region Send()
public void Send(){
//MessageCenter Send Message
MessageCenter.Instance.SendMessage(this);
}
#endregion
}
}
2.19、MessageCenter
++++MessageCenter:消息中心。
++++Scripts/YanlzFramework/Common/Message/MessageCenter.cs
++2.19.1、MessageCenter.cs(消息中心)
//消息中心:(YanlzFramework/Common/Message/MessageCenter.cs)
using System;
namespace YanlzFramework{
public class MessageCenter : Singleton<MessageCenter>{
private Dictionary<string, List<MessageEvent>> dicMessageEvents = null;
public override void Init(){
dicMessageEvents = new Dictionary<string, List<MessageEvent>>();
}
#region Add & Remove Listener
public void AddListener(string messageName, MessageEvent messageEvent){
Debug.Log(“AddListener Name: ” + messageName);
List<MessageEvent> list = null;
if(dicMessageEvents.ContainsKey(messageName)){
list = dicMessageEvents[messageName];
}else{
list = new List<MessageEvent>();
dicMessageEvents.Add(messageName, list);
}
//list.Add(messageEvent);
//debug: no same messageEvent then add
if(!list.Contains(messageEvent)){
list.Add(messageEvent);
}
}
public void RemoveListener(string messageName, MessageEvent messageEvent){
if(dicMessageEvent.ContainsKey(messageName)){
List<MessageEvent> list = dicMessageEvents[messageName];
if(list.Contains(messageEvent)){
list.Remove(messageEvent);
}
if(list.Count <= 0){
dicMessageEvent.Remove(messageName);
}
}
}
public void RemoveAllListener(){
dicMessageEvents.Clear();
}
#endregion
#region Send Message
public void SendMessage(Message message){
DoMessageDispatcher(message);
}
public void SendMessage(string name, object sender){
SendMessage(new Message(name, sender,));
}
public void SendMessage(string message, object sender, object content){
SendMessage(new Message(name, sender, content));
}
public void SendMessage(string name, object sender, object content, params object[] dicParams){
SendMessage(new Message(name, sender, content, dicParams));
}
#region :基于NetMessageType的消息处理:供参考
public void AddListener(NetMessageType messageType, MessageEvent messageEvent){
AddListener(messageType.ToString(), MessageEvent);
}
public void RemoveListener(NetMessageType messageType, MessageEvent messageEvent){
RemoveListener(messageType.ToString(), messageEvent);
}
public void SendMessage(NetMessageType messageType, object sender){
SendMessage(new Message(messageType.ToString(), sender,));
}
public void SendMessage(NetMessageType messageType, object sender, object content){
SendMessage(new Message(messageType.ToString(), sender, content));
}
public void SendMessage(NetMessageType messageType, object sender, object content, params object[] dicParams){
SendMessage(new Message(messageType.ToString(), sender, content, dicParams));
}
#endregion
private void DoMessageDispatcher(Message message){
Debug.Log(“DoMessageDispatcher Name:” + message.Name);
if(dicMessageEvents == null || dicMessageEvent.ContainsKey(message.Name)){
return;
}
List<MessageEvent> list = dicMessageEvents[message.Name];
for(int i = 0; i < list.Count; i++){
MessageEvent messageEvent = list[i];
if(null != messageEvent){
messageEvent(message);
}
}
}
#endregion
}
}
2.20、MessageType
++++MessageType:消息类型。
++++Scripts/YanlzFramework/Message/MessageType.cs
++2.21.1、MessageType.cs(消息类型)
//消息类型:(Scripts/YanlzFramework/Message/MessageType.cs)
using System;
namespace YanlzFramework{
public class MessageType{
public static string Net_MessageTestOne = “Net_MessageTestOne”;
public static string Net_MessageTestTwo = “Net_MessageTestTwo”;
}
public Enum OldMessageType : int{
MessageTestOne = 1,
MessageTestTwo ,
}
public Enum NetMessageType : int{
Net_MessageTestOne = 1,
Net_MessageTestTwo ,
}
}
2.21、EventTriggerListener
++++EventTriggerListener:事件监听。
++++Scripts/YanlzFramework/Common/Tools/EventTriggerListener.cs
++2.21.1、EventTriggerListener.cs(事件监听)
//事件监听:(YanlzFramework/Common/Tools/EventTriggerListener.cs)
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
namespace YanlzFramework{
public class TouchHandle{
private event OnTouchEventHandle eventHandle = null;
private object[] handleParmas;
public TouchHandle(OnTouchEventHandle _handle, params object[] _params){
SetHandle(_handle, _params);
}
public TouchHandle(){
}
public void SetHandle(OnTouchEventHandle _handle, params object[] _params){
DestroyHandle();
eventHandle += _handle;
handleParams = _params;
}
public void CallEventHandle(GameObject _sender, object _args){
if(null != eventHandle){
eventHandle(_sender, _args, handleParams);
}
}
public void DestroyHandle(){
if(null != eventHandle){
eventHandle -= eventHandle;
eventHandle = null;
}
}
}
public class EventTriggerListener : MonoBehaviour,
IPointerClickHandler,
IPointerDownHandler,
IPointerUpHander,
IPointerEnterHandler,
IPointerExitHandler,
ISelectHandler,
IUpdateSelectedHandlder,
IDeselectHandler,
IDragHandler,
IEndDragHandler,
IDropHnadler,
IScrollHandler,
IMoveHandlder{
public TouchHandler onClick;
public TouchHandler onDoubleClick; //双击
public TouchHandler onDown;
public TouchHandler onEnter;
public TouchHandler onExit;
public TouchHandler onUp;
public TouchHandler onSelect;
public TouchHandler onUpdateSelect;
public TouchHandler onDeSelect;
public TouchHandler onDrag;
public TouchHandler onDragEnd;
public TouchHandler onDrop;
public TouchHandler onScroll;
public TouchHandler onMove;
static public EventTriggerListener Get(GameObject go){
//EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
//EventTriggerListener listener = go.GetOrAddComponent<EventTriggerListener>();
//return listener;
return go.GetOrAddComponent<EventTriggerListener>();
}
void OnDestroy(){
this.RemoveAllHandle();
}
private void RemoveAllHandle(){
this.RemoveHandle(onClick);
this.RemoveHandle(onDoubleClick);
this.RemoveHandle(onDown);
this.RemoveHandle(onEnter);
this.RemoveHandle(onExit);
this.RemoveHandle(onUp);
this.RemoveHandle(onDrop);
this.RemoveHandle(onDrag);
this.RemoveHandle(onDragEnd);
this.RemoveHandle(onScroll);
this.RemoveHandle(onMove);
this.RemoveHandle(onUpdateSelect);
this.RemoveHandle(onSelect);
this.RemoveHandle(onDeSelect);
}
private void RemoveHandle(TouchHandle _handle){
if(null != _handle){
_handle.DestroyHandle();
_handle = null;
}
}
#region IDragHandler implementation
public void OnDrag(PointerEventData eventData){
if(onDrag != null){
onDrag.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
#region IEndDragHandler implementation
public void OnEndDrag(PointerEventData eventData){
if(onDragEnd != null){
onDragEnd.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
#region IDropHandler implementation
public void OnDrop(PointerEventData eventData){
if(onDrop != null){
onDrop.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
#region IPointerClickHandler implementation
public void OnPointerClick(PointerEventData eventData){
if(null != onClick){
onClick.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
#region IPointerDownHandler implementation
public void OnPointerDown(PointerEventData eventData){
if(null != onDown){
onDown.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
#region IPointerUpHandler implementation
public void OnPointerUp(PointerEventData eventData){
if(onUp != null){
onUp.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
#region IPointerEnterHandler implementation
public void OnPointerEnter(PointerEventData eventData){
if(onEnter != null){
onEnter.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
#region IPointerExitHandler implementation
public void OnPointerExit(PointerEventData eventData){
if(onExit != null){
onExit.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
#region ISelectHandler implementation
public void OnSelect(BaseEventData eventData){
if(onSelect != null){
onSelect.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
#region IUpdateSelecteHandler implementation
public void OnUpdateSelected(BaseEventData eventData){
if(onUpdateSelect != null){
onUpdateSelect.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
#region IDeselectHandler implementation
public void OnDeselect(BaseEventData eventData){
if(onDeSelect != null){
onDeSelect.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
#region IScrollHandler implementation
public void OnScroll(PointerEventData eventData){
if(onScroll != null){
onScroll.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
#region IMoveHandler implementation
public void OnMove(AxisEventData eventData){
if(onMove != null){
onMove.CallEventHandle(this.gameObject, eventData);
}
}
#endregion
public void SetEventHandle(EnumTouchEventType _type, OnTouchEventHandle _handle, param object[] _params){
switch(_type){
case EnumTouchEventType.OnClick:{
if(null == onClick){
onClick = new TouchHandle();
}
onClick.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnDoubleClick:{
if(null == onDoubleClick){
onClick = new TouchHandle();
}
onClick.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnDown:{
if(null == onDown){
onDown= new TouchHandle();
}
onDown.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnUp:{
if(null == onUp){
onUp = new TouchHandle();
}
onUp.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnEnter:{
if(null == onEnter){
onEnter = new TouchHandle();
}
onEnter.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnExit:{
if(null == onExit){
onExit = new TouchHandle();
}
onExit.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnDrag:{
if(null == onDrag){
onDrag = new TouchHandle();
}
onDrag.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnDrop:{
if(null == onDrop){
onDrop = new TouchHandle();
}
onDrop.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnDragEnd:{
if(null == onDragEnd){
onDragEnd = new TouchHandle();
}
onDragEnd.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnSelect:{
if(null == onSelect){
onSelect = new TouchHandle();
}
onSelect.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnUpdateSelect:{
if(null == onUpdateSelect){
onUpdateSelect = new TouchHandle();
}
onUpdateSelect.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnDeSelect:{
if(null == onDeSelect{
onDeSelect = new TouchHandle();
}
onDeSelect.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnScroll:{
if(null == onScroll){
onScroll = new TouchHandle();
}
onScroll.SetHandle(_handle, _params);
break;
}
case EnumTouchEventType.OnMove:{
if(null == onMove){
onMove = new TouchHandle();
}
onMove.SetHandle(_handle, _params);
break;
}
}
}
//Use this for initialization
void Start(){
}
}
}
2.22、MethodExtension
++++MethodExtenson:方法扩展。
++++Scripts/YanlzFramework/Common/Extended/MethodExtension.cs
++2.22.1、MethodExtension.cs(方法扩展)
//方法扩展:(YanlzFramework/Common/Extended/MethodExtension.cs)
using System;
using UnityEngine;
namespace YanlzFramework{
static public class MethodExtension{
static public T GetOrAddComponent<T>(this GameObject go) where T : Component{
T ret = go.GetComponent<T>();
if(null == ret){
ret = go.AddComponent<T>();
}
return ret;
}
}
}
2.23、TestOneModule
++++TestOneModule:数据模块。
++++Scripts/GameLogic/Module/TestOneModule.cs
++2.23.1、TestOneModule.cs(数据模块)
//数据模块:(Scripts/GameLogic/Module/TestOneModule.cs)
using System;
using YanlzFramework;
public class TestOneModule : BaseModule{
public int Gold{ set; private set; }
public TestOneModule(){
this.AutoRegister = true;
}
protected override void OnLoad(){
MessageCenter.Instance.AddListener(MessageType.Net_MessageTestOne, NetUpdateGold);
base.OnLoad();
}
protected override void OnRelease(){
MessageCenter.Instance.RemoveListener(MessageType.Net_MessageTypeOne, NetUpdateGold);
base.OnRelease();
}
//模拟网络消息更新
private void NetUpdateGold(Message msg){
int gold = (int)msg[“gold”];
if(gold >= 0){
Gold = gold;
Message message = new Message(“AutoUpdateGold”, this);
message[“gold”] = gold;
message.Send();
}
}
}
2.24、PropertyItem
++++PropertyItem:属性类。
++++Scripts/YanlzFramework/BaseClass/PropertyItem.cs
++2.24.1、PropertyItem.cs(属性类)
//属性类:(YanlzFramework/BaseClass/PropertyItem.cs)
using System;
namespace YanlzFramework{
public class PropertyItem{
public int ID{ get; set; }
private object content;
private object rawContent;
private bool canRandom = false;
private int curRandomInt;
private float curRandomFloat;
private Type propertyType;
public IDynamicProperty Owner = null; //owner
public object Content{
get{
return GetContent();
}
get{
if(value != GetContent()){
object oldContent = GetContent();
SetContent(value);
if(Owner != null){
Owner.DoChangeProperty(ID, oldContent, value);
}
}
}
}
public void SetValueWithoutEvent(object content){
if(content != GetContent()){
object oldContent = GetContent();
SetContent(content);
}
}
public object RawContent{
get{
return rawContent;
}
}
public PropertyItem(int id, object content){
ID = id;
setContent(content);
rawContent = content;
propertyType = content.GetType();
if(propertyType == System.Int32 || propertyType == System.Single){
canRandom = true;
}
}
private void SetContent(object content){
rawContent = content;
if(canRandom){
if(propertyType == typeof(System.Int32)){
curRandomInt = UnityEngine.Random.Range(1, 1000);
this.content = (int)content + curRandomInt;
}else if(propertyType == typeof(System.Single)){
curRandomFloat = UnityEngine.Random.Range(1.0f, 1000.0f);
this.content = (float)content + curRandomFloat;
}
}else{
this.content = content;
}
}
private object GetContent(){
if(canRandom){
if(propertyType == typeof(System.Int32)){
int ret = (int)this.content - curRandomInt;
if(ret != rawContent){
Message message = new Message(“PropertyItemDataException”, this, ID);
message.Send();
}
return ret;
}else if(propertyType == typeof(System.Single)){
float ret = (float)this.content - curRandomFloat;
if(ret != rawContent){
Message message = new Message(“PropertyItemDataException”, this, ID);
message.Send();
}
return ret;
}
}
return this.content;
}
}
}
2.25、IDynamicProperty
++++IDynamicProperty:属性。
++++Scripts/YanlzFramework/Interface/IDynamicProperty.cs
++2.25.1、IDynamicProperty.cs(属性)
//属性:(YanlzFramework/Interface/IDynamicProperty.cs)
using System;
namespace YanlzFramework{
public interface IDynamicProperty{
void DoChangeProperty(int id, object oldValue, object newValue);
PropertyItem GetProperty(int id);
}
}
2.26、BaseActor
++++BaseActor:角色基类。
++++Scripts/YanlzFramework/BaseClass/BaseActor.cs
++2.26.1、BaseActor.cs(角色基类)
//角色基类:(YanlzFramework/BaseClass/BaseActor.cs)
using System;
using System.Collections.Generic;
namespace YanlzFramework{
public class BaseActor : IDynamicProperty{
protected Dictionary<int, PropertyItem> dicProperty;
public event PropertyChangedHandle PropertyChanged;
public EnumActorType ActorType{ set; get; }
public int ID{ set; get; }
private BaseScene currentScene;
public BaseScene CurrentScene{
set{
//add Change Scene Logic ...
currentScene = value;
}
get{
return currentScene;
}
}
public virtual void AddProperty(EnumPropertyType propertyType, object content){
AddProperty((int)propertyType, content);
}
public virtual void AddProperty(int id, object content){
PropertyItem item = new PropertyItem(id, content);
AddProperty(item);
}
public virtual void AddProperty(PropertyItem property){
if(null == dicProperty){
dicProperty = new Dictionary<int, PropertyItem>();
}
if(dicProperty.ContainsKey(property.ID)){
//remove same property
}
dicProperty.Add(property.ID, property);
property.Owner = this;
}
public void RemoveProperty(EnumPropertyType propertyType){
RemoveProperty((int)propertyType);
}
public void RemoveProperty(int id){
if(null != dicProperty && dicProperty.ContainsKey(id)){
dicProperty.Remove(id);
}
}
public void ClearProperty(){
if(null != dicProperty){
dicProperty.Clear();
dicProperty = null;
}
}
public virtual PropertyItem GetProperty(EnumPropertyType propertyType){
return GetProperty((int)properyType);
}
protected virtual void OnPropertyChanged(int id, object oldValue, object newValue){
//add update ....
//if(id == (int)EnumPropertyType.HP)
}
#region IDynamicProperty implementation
public void DoChangeProperty(int id, object oldValue, object newValue){
OnPropertyChanged(id, oldValue, newValue);
if(null != PropertyChanged){
PropertyChanged(this, id, oldValue, newValue);
}
}
public PropertyItem GetProperty(int id){
if(null == dicProperty){
return null;
}
if(dicProperty.ContainsKey(id)){
return dicProperty[id];
}
Debug.LogWarning(“Actor dicProperty non Property ID:” + id);
return null;
}
#endregion
public BaseActor(){
}
}
}
2.27、BaseScene
2.27、BaseScene
++2.27、BaseScene
++++BaseScene:场景基类。
++++Scripts/YanlzFramework/BaseClass/BaseScene.cs
++2.27.1、BaseScene.cs(场景基类)
//场景基类:(YanlzFramework/BaseClass/BaseScene.cs)
using System;
namespace YanlzFramework{
public class BaseScene : BaseModule{
protected List<BaseActor> actorList = null;
public BaseScene(){
actorList = new List<BaseActor>();
}
public void AddActor(BaseActor actor){
if(null != actor && !actorList.Contains(actor)){
actorList.Add(actor);
actor.CurrentScene = this;
actor.PropertyChanged += OnActorPropertyChanged;
//actor.Load();
}
}
public void RemoveActor(BaseActor actor){
if(null != actor && actorList.Contains(actor)){
actorList.Remove(actor);
actor.PropertyChanged -= OnActorPropertyChanged;
//actor.Release();
actor = null;
}
}
public virtual BaseActor GetActorByID(int id){
if(null != actorList && actorList.Count > 0){
for(int i = 0; i < actorList.Count; i++){
if(actorList[i].ID == id){
return actorList[i];
}
}
}
return null;
}
protected void OnActorPropertyChanged(BaseActor actor, int id, object oldValue, object newValue){
}
}
}
2.28、SceneManager
++++SceneManager:场景管理器。
++++Scripts/YanlzFramework/Manager/SceneManager.cs
++2.28.1、SceneManager.cs(场景管理器)
//场景管理器:(YanlzFramework/Manager/SceneManager.cs)
using System;
namespace YanlzFramework{
public class SceneManager : Singleton<SceneManger>{
#region SceneInfoData class
public class SceneInfoData{
public Type SceneType{ get; private set; }
public string SceneName{ get; private set; }
public object[] Params{ get; private set; }
public SceneInfoData(string _sceneName, Type _sceneType, params object[] _params){
this.SceneType = _sceneType;
this.SceneName = _sceneName;
this.Params = _params;
}
}
#endregion
private Dictionary<EnumSceneType, SceneInfoData> dicSceneInfos = null;
private BaseScene currentScene = new BaseScene();
public BaseScene CurrentScene{
get{
return currentScene;
}
set{
currentScene = value;
if(null != currentScene){
currentScene.Load();
}
}
}
public EnumSceneType LastSceneType{ get; set; }
public EnumSceneType ChangeSceneType{ get; private set; }
private EnumUIType sceneOpenUIType = EnumUIType.None;
private object[] sceneOpenUIParams = null;
public override void Init(){
dicSceneInfos = new Dictionary<EnumSceneType, SceneInfoData>();
}
public void RegisterAllScene(){
RegisterScene(EnumSceneType.StartGame, “StartGameScene”, null, null);
RegisterScene(EnumSceneType.LoginScene, “LoginScene”, typeof(BaseScene), null);
RegisterScene(EnumSceneType.MainScene, “MainScene”, null, null);
RegisterScene(EnumSceneType.CopyScene, “CopyScene”, null, null);
}
public void RegisterScene(EnumSceneType _sceneID, string _sceneName, Type _sceneType, params object[] params){
if(_sceneType == null || _sceneType.BaseType != typeof(BaseScene)){
throw new Exception(“Register scene type must not null and extends BaseScene”);
}
if(dicSceneInfos.ContainsKey(_sceneID)){
SceneInfoData sceneInfo = new SceneInfoData(_sceneName, _sceneType, _params);
dicSceneInfos.Add(_sceneID, sceneInfo);
}
}
public void UnRegisterScene(EnumSceneType _sceneID){
if(dicSceneInfos.ContainsKey(_sceneID)){
dicSceneInfos.Remove(_sceneID);
}
}
public bool IsRegisterScene(EnumSceneType _sceneID){
return dicSceneInfos.ContainsKey(_sceneID);
}
internal BaseScene GetBaseScene(EnumSceneType _sceneType){
Debug.Log(“GetBaseScene sceneId = ” + _sceneType.ToString());
SceneInfoData sceneInfo = GetSceneInfo(_sceneType);
if(sceneInfo == null || sceneInfo.SceneType == null){
return null;
}
BaseScene scene = System.Activator.CreateInstance(sceneInfo.SceneType) as BaseScene;
return scene;
//BaseScene scene = Game.Instance.GetBaseScene(Game.Instance.ChangeSceneId);
//Game.Instance.CurrentScene = scene;
//scene.Load();
}
public SceneInfoData GetSceneInfo(EnumSceneType _sceneID){
if(dicSceneInfos.ContainsKey(_sceneID)){
return dicSceneInfos[_sceneID];
}
Debug.LogError(“This Scene is not register! ID: ” + _sceneID.ToString());
return null;
}
public string GetSceneName(EnumSceneType _sceneID){
if(dicSceneInfos.ContainsKey(_sceneID)){
return dicSceneInfo[_sceneID].SceneName;
}
Debug.LogError(“This Scene is not register! ID: ” + _sceneID.ToString());
return null;
}
public void ClearScene(){
dicSceneInfos.Clear();
}
#region Change Scene
public void ChangeSceneDirect(EnumSceneType _sceneType){
UIManager.Instance.CloseUIAll();
if(CurrentScene != null){
CurrentScene.Release();
CurrentScene = null;
}
LastSceneType = ChangeSceneType;
ChangeSceneType = _sceneType;
string sceneName = GetSceneName(_sceneType);
//change scene
CoroutineController.Instance.StartCoroutine(AsyncLoadScene(sceneName));
}
public void ChangeSceneDirect(EnumSceneType _sceneType, EnumUIType _uiType, params object[] _params){
sceneOpenUIType = _uiType;
sceneOpenUIParams = _params;
if(LastSceneType == _sceneType){
if(sceneOpenUIType == EnumUIType.None){
return;
}
UIManager.Instance.OpenUI(sceneOpenUIType, sceneOpenUIParams);
sceneOpenUIType = EnumUIType.None;
}else{
ChangeSceneDirect(_sceneType);
}
}
private IEnumerator<AsyncOperation> AsyncLoadScene(string sceneName){
AsyncOperation oper = Application.LoadLevelAsync(sceneName);
yield return oper;
//message send
if(sceneOpenUIType != EnumType.None){
UIManager.Instance.OpenUI(sceneOpenUIType, sceneOpenUIParams);
sceneOpenUIType = EnumUIType.None;
}
}
#endregion
public void ChangeScene(EnumSceneType _sceneType){
UIManager.Instance.CloseUIAll();
if(CurrentScene != null){
CurrentScene.Release();
CurrentScene = null;
}
LastSceneType = ChangeSceneType;
ChangeSceneType = _sceneType;
string sceneName = GetSceneName(_sceneType);
//change loading scene
CoroutineController.Instance.StartCoroutine(AsyncLoadOtherScene());
}
public void ChangeScene(EnumSceneType _sceneType, EnumUIType _uiType, params object[] _params){
sceneOpenUIType = _uiType;
sceneOpenUIParams = _params;
if(LastSceneType == _sceneType){
if(sceneOpenUIType == EnumUIType.None){
return;
}
UIManager.Instance.OpenUI(sceneOpenUIType, sceneOpenUIParams);
sceneOpenUIType = EnumUIType.None;
}else{
ChangeScene(_sceneType);
}
}
private IEnumerator AsyncLoadOtherScene(){
string sceneName = GetSceneName(EnumSceneType.LoadingScene);
AsyncOperation oper = Application.LoadLevelAsync(sceneName);
yield return oper;
//message send
if(oper.isDone){
GameObject go = GameObject.Find(“LoadingScenePanel”);
LoadingSceneUI loadingSceneUI = go.GetComponent<LoadingSceneUI>();
BaseScene scene = CurrentScene;
if(null != scene){
scene.CurrentSceneId = ChangeSceneId;
}
//检测是否注册该场景
if(!SceneManager.Instance.isRegisterScene(ChangeSceneId)){
Debug.LogError(“没有注册此场景:” + ChangeSceneId.ToString());
}
LoadingSceneUI.Load(ChangeSceneId);
LoadingSceneUI.LoadCompleted += SceneLoadCompleted;
}
}
void SceneLoadCompleted(object sender, EventArgs e){
Debug.Log(“切换场景完成 + ” + sender as String);
//场景切换完成
//MessageCenter.Instance.SendMessage(MessageType.GAMESCENE_CHANGECOMPLETE, this, null, false);
//有要打开的UI
if(sceneOpenUIType != EnumUIType.None){
UIManager.Instance.OpenUI(false, sceneOpenUIType, sceneOpenUIParams);
sceneOpenUIType = EnumUIType.None;
}
}
//加载场景
private IEnumerator AsyncLoadedNextScene(){
string fileName = SceneManager.Instance.GetSceneName(ChangeSceneId);
AsyncOperation oper = Application.LoadLevelAsync(fileName);
yield return oper;
if(oper.isDone){
if(LoadCompleted != null){
LoadCompleted(changeSceneId, null);
}
}
}
}
}
2.29、MailUI
++++MailUI:邮件界面。
++++Scripts/GameLogic/UI/Mail/MailUI.cs
++2.29.1、MailUI.cs(邮件界面)
//邮件界面:(Scripts/GameLogic/UI/Mail/MailUI.cs)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using YanlzFramework;
using UnityEngine.UI;
public class MailUI : BaseUI{
private Dictionary<EnumMailType, UIButton> dicTitleButton = null;
public Button button_Close;
public Button button_MailSystem;
public Button button_MailBattleReport;
public Button button_MailAll;
public Button button_GetRewardAll;
public Button button _DeleteAll;
public UIScrollView scrollView;
public UIGrid grid_Cell;
private Dictionary<int, GameObject> obj_Items = new Dictionary<int, GameObject>();
private MailModule mailModule = null;
private EnumMailType currMailType = EnumMailType.mail_system;
#region implemeted abstract members of BaseUI
public override EnumUIType GetUIType(){
return EnumUIType.MailUI;
}
#endregion
protected override void OnLoad(){
base.SetDepthToTop();
mailModule = ModuleManager.Instance.Get<MailModule>();
obj_Items = new Dictionary<int, GameObject>();
for(int i = 0; i < 4; i++){
int cellId = i + 1;
GameObject objCell = ResourceManager.Instance.ResourceLoad(UIResourcePath.UI_CONTROLS_PREFAB + (“mail_Item” + cellId)) as GameObject;
obj_Items.Add(cellId, objCell);
}
EventTriggerListener.SetEventHandle(button_Close.gameObject, EnumTouchEventType.OnClick, ButtonOnClick_Close, null, null);
EventTriggerListener.SetEventHandle(button_MailSystem.gameObject, EnumTouchEventType.OnClick, ButtonOnClick_MailType, EnumMailType.mail_system, null);
EventTriggerListener.SetEventHandle(button_mailBattleReport.gameObject, EnumTouchEventType.OnClick, ButtonOnClick_MailType, EnumMailType.mail_battleReport, null);
EventTriggerListener.SetEventHandle(button_MailAll.gameObject, EnumTouchEventType.OnClick, ButtonOnClick_MailType, EnumMailType.mail_all, null);
EventTriggerListener.SetEventHandle(button_GetRewardAll.gameObject, EnumTouchEventType.OnClick, ButtonOnClick_GetRewardAll, null, null);
EventTriggerListener.SetEventHandle(button_DeleteAll.gameObject, EnumTouchEventType.OnClick, ButtonOnClick_DeleteAll, null, null);
dicTitleButton = new Dictionary<EnumMailType, UIButton>();
dicTitleButton.Add(EnumMailType.mail_system, button_MailSystem);
dicTitleButton.Add(EnumMailType.mail_battleReport, button_MailBattleReport);
dicTitleButton.Add(EnumMailType.mail_all, button_MailAll);
MessageCenter.Instance.AddListener(“ResponseRefreshMailItemData”, ResponseRefreshMailItemData);
this.SetMailPageType(EnumMailType.mail_system, true);
base.OnLoad();
}
protected override void SetUI(params object[] uiParams){
base.SetUI(uiParams);
}
protected override void OnLoadData(){
UIManager.Instance.SetUIToMessageBlock(this.gameObject);
base.OnLoadData();
}
protected override void OnRelease(){
MessageCenter.Instance.RemoeListener(“ResponseRefreshMailItemData”, ResponseRefreshMailItemData);
base.OnRelease();
}
private void ButtonOnClick_Close(GameObject obj, object args, object param1, object param2){
UIManager.Instance.CloseUI(GetUIType());
}
private void ButtonOnClick_MailType(GameObject obj, object args, object param1, object param2){
EnumMailType type = (EnumMailType)param1;
if(currMailType == type){
return;
}
currMailType = type;
this.SetMailPageType(currMailType, true);
}
private void SetMailPageType(EnumMailType type, bool isReset){
this.SetTitleButtonState(type);
StartCoroutine(this.RefreshMailItemList(mailModule.GetAllMailDataList(type), isReset));
}
private void ButtonOnClick_GetRewardAll(GameObject obj, object args, object param1, object param2){
mailModule.sendGetMailRewardDataMessage(2, currMailType, 0);
}
private void ButtonOnClick_DeleteAll(GameObject obj, object args, object param1, object param2){
UIManager.Instance.OpenDoubleMessageBox(“删除所有邮件”, ButtonOnClick_DeleteAllConfire, null);
}
private void ButtonOnClick_DeleteAllConfire(GameObject obj, object args, object param1, object param2){
mailModule.sendGetMailRewardDataMessage(2, currMailType, 0);
}
private void ResponseRefreshMailItemData(Message message){
this.SetMailPageType(currMailType, false);
}
public IEnumerator RefreshMailItemList(List<MailInfoData> filterList, bool isReset){
//先销毁元素
foreach(Transform tf in grid_Cell.GetChildList()){
DestroyObject(tf.gameObject);
}
if(isReset){
scrollView.SetDragAmount(0, 0, false);
}
for(int i = 0; i < filterList.Count; i++){
MailInfoData infoData = filterList[i];
this.AddItemCell(infoData);
}
yield return new WaitForFixedUpdate();
grid_Cell.Reposition();
}
private void AddItemCell(MailInfoData infoData){
GameObject objCell = NGUITools.AddChild(grid_Cell.gameObject, obj_Items[infoData.mailPrototype.cellID]);
switch(infoData.mailPrototype.cellID){
case 1:{
MailUICell_1 mailUICell_1 = objCell.GetComponent<MailUICell_1>();
mailUICell_1.Show(infoData);
break;
}
}
}
}
2.30、MailRewardUI
++++MailRewardUI:邮件奖励界面。
++++Scripts/GameLogic/UI/Mail/MailRewardUI.cs
++2.30.1、MailRewardUI.cs(邮件奖励界面)
//邮件奖励界面:(Scripts/GameLogic/UI/Mail/MailRewardUI.cs)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using YanlzFramework;
using UnityEngine.UI;
public class MailRewardUI : BaseUI{
public UIButton button_Close;
public UIButton button_Confire;;
public UIScrollView scrollView;
public UIGrid grid_Cell;
private GameObject objCell;
private MailInfoData mailInfoData = null; //奖励
#region implemented abstract members of BaseUI
public override EnumUIType GetUIType(){
return EnumUIType.MailRewadUI;
}
#endregion
protected override void OnLoad(){
base.SetDepthToTop();
objCell = ResourceManager.Instance.ResourceLoad(UIResourcePath.UI_CONTROLS_PREFAB + “mail_reward”, ....);
EventTriggerListener.SetEventHandle(button_Confire.gameObject, EnumTouchEventType.OnClick, ButtonOnClick....);
EventTriggerListener.SetEventHandle(button_Close.gameObject, EnumTouchEventType.OnClick, ButtonOnClick....);
base.OnLoad();
}
protected override void SetUI(params object[] uiParams){
if(uiParmas != null && uiParams.Length > 0){
mailInfoData = (MailInfoData)uiParams[0];
}
base.SetUI(uiParams);
}
protected override void OnLoadData(){
UIManager.Instance.SetUIToMessageBlock(this.gameObject);
this.Show();
base.OnLoadData();
}
protected override void OnRelease(){
base.OnRelease();
}
private void ButtonOnClick_Confire(GameObject obj, object args, object[] _params){
UIManager.Instance.CloaseUI(GetUIType());
MailModule mailModule = ModuleManager.Instance.Get<MailModule>();
mailModule.sendGetMailRewardDataMessage(1, EnumMailType.mail_none, mailInfoData.OnlyId);
}
private void ButtonOnClick_Close(GameObject obj, object args, object[] _params){
UIManager.Instance.CloseUI(GetUIType());
}
public void Show(){
StartCoroutine(this.RefreshItemList());
}
public IEnumerator RefreshItemList(){
//先销毁元素
foreach(Transform tf in grid_Cell.GetChildList()){
DestroyObject(tf.gameObject);
}
scrollView.SetDragAmount(0, 0, false);
for(int i = 0; i < mailInfoData.rewardController.rewardInfoList.Count; i++){
ItemBaseDataInfo itemDataInfo = mailInfoData.rewardController.rewardInfoList[i];
GameObject obj = NGUITools.AddChild(grid_Cell.gameObject, objCell);
MailRewardUICell itemCell = obj.GetComponent<MailRewardUICell>();
itemCell.Show(itemDataInfo);
}
yield return new WaitForFixedUpdate();
gird_Cell.Reposition();
}
}
2.31、MailModule
++++MailModule:邮件模块。
++++Scripts/GameLogic/Module/Mail/MailMoudle.cs
++2.31.1、MailModule(邮件模块)
//邮件模块(Scripts/GameLogic/Module/Mail/MailModule.cs)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using YanlzFramework;
public class MailModule : BaseModule{
private List<MailInfoData> allMailDataList = null; //邮件列表
private List<long> allUnReadMailList = null; //未读列表
public MailMoudle(){
AutoRegister = true;
allMailDataList = new List<MailInfoData>();
allUnReadMailList = new List<long>();
}
protected override void OnLoad(){
MessageCenter.Instance.AddListener(“getAllMailData”, ResponseGetAllMailData);
MessageCenter.Instance.AddListener(“getMailReward”, ResponseGetMailRewardData);
MessageCenter.Instance.AddListener(“deleteMailData”, ResponseDeleteMailData);
base.OnLoad();
}
protected override void OnRelease(){
MessageCenter.Instance.RemoveListener(“getAllMailData”, ResponseGetAllMailData);
MessageCenter.Instance.RemoveListener(“getMailReward”, ResponseGetMailRewardData);
MessageCenter.Instance.RemoveListener(“deleteMailData”, ResponseDeleteMailData);
base.OnRelease();
}
private void LoadAllMailData(Dictionary<string, object> jsonData){
//数据
if(jsonData.ContainsKey(“mails”)){
allMailDataList.Clear();
List<object> allItem = jsonData[“mails”] as List<object>;
for(int i = 0; i < allItem.Count; i++){
Dictionary<string, object> dicData = allItem[i] as Dictionary<string, object>;
MailInfoData infoData = MailInfoData.GetMailInfoData(dicData);
allMailDataList.Add(infoData);
}
}
//未读数据
if(jsonData.ContainsKey(“unReadMailId”)){
allUnReadMailList.Clear();
List<object> unReadList = jsonData[“unReadMailId”] as List<object>;
for(int i = 0; i < unReadList.Count; i++){
long id = long.Parse(unReadList[i].ToString());
allUnReadMailList.Add(id);
}
}
}
//获取全局列表
public List<MailInfoData> GetAllMailDataList(){
return this.allMailDataList;
}
public List<MailInfoData> GetAllMailDataList(EnumMailType type){
List<MailInfoData> tempList = new List<MailInfoData>();
if(type == EnumMailType.mail_all){
tempList.AddRange(this.allMailDataList);
}else{
for(int i = 0; i < this.allMailDataList.Count; i++){
MailInfoData infoData = this.allMailDataList[i];
if(infoData.MailType == (int)type){
tempList.Add(infoData);
}
}
}
tempList.Sort((x,y)=>{
return x.CreateTime.CompareTo(y.CreateTime);
});
return tempList;
}
public bool IsReadMailState(long onlyId){
if(this.allUnReadMailList.Contains(onlyId)){
return false;
}
return true;
}
//获取所有邮件数据
public void SendGetAllMailDataMessage(){
Message message = new Message(“getAllMailData”, this, null);
message[“NOTIFICATION_CONNECT_UI”] = true;
message.Send(true, true, null);
}
private void ResponseGetAllMailData(Message message){
Debug.Log(“ResponseGetAllMailData”);
Dictionary<string, object> dicData = message.Content as Dictionary<string, object>;
this.LoadAllMailData(dicData);
UIManager.Instance.OpenUI(false, EnumUIType.MailUI);
}
public void SendGetMailRewardDataMessage(int operType, EnumMailType mailType, long onlyId){
Message message = new Message(“getMailReward”, this, null);
message[“NOTIFICATION_CONNECT_UI”] = true;
message[“operType”] = operType;
if(operType == 1){
message[“onlyId”] = (int)onlyId; //获取单个
}else if(operType == 2){
message[“mailType”] = (int)mailType; //获取全部
}
message.Send(true, true, null);
}
private void ResponseGetMailRewardData(Message message){
Debug.Log(“ResponseGetMailRewardData”);
Dictionary<string, object> dicData = message.Content as Dictionary<string, object>;
this.LoadAllMailData(dicData);
MessageCenter.Instance.SendMessage(“ResponseRefreshMailItemData”, this, null, false);
}
}
2.32、MailData
++++MailData:邮件数据结构。
++++Scripts/GameLogic/Module/Mail/MailData.cs
++2.32.1、MailData
邮件数据结构(Scripts/GameLogic/Module/Mail/MailData.cs)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum EnumMailType{
mail_none = 0,
mail_system = 1,
mail_battleReport = 2,
mail_all = 3,
}
public class MailInfoData{
public long OnlyId{ private set; get; } //唯一ID
public int MailCfgId{ private set; get; } //邮件配置目标ID
public int MailType{ private set; get; } //邮件类型
public long CreateTime{ private set; get; } //创建时间,秒数
public long RewardTime{ private set; get; } //领取奖励时间
public long OutDateTime{ private set; get; } //过期时间,秒数
public int State{ private set; get; } //0:未领奖,1:已领奖励
public List<string> paramList = new List<string>(); //邮件参数集合
public RewardController rewardController = new RewardController(); //奖励数据
public MailPrototype mailPrototype = null;
public string Content{ private set; get; } //邮件内容
public MailInfoData(int onlyId, int cfgId, int type, long createTime, long rewardTime, long outDaeTime, int state){
this.OnlyId = onlyId;
this.MailCfgId = cfgId;
this.MailType = type;
this.CreateTime = createTime;
this.RewardTime = rewardTime;
this.OutDateTime = outDateTime;
this.State = state;
if(param != null){
this.paramList = param;
}
if(reward != null){
rewardController = reward;
}
mailPrototype = ConfigManager.Instance.mailPrototypes.Find(this.MailCfgId);
//设置内容,如果参数不够
if(this.paramList.Count < mailPrototype.paramCount){
int subCount = mailPrototype.paramCount - this.paramList.Count;
for(int i = 0; i < subCount; i++){
this.paramList.Add(“”);
}
}
this.Content = string.Format(mailPrototype.content, this.paramList.ToArray());
}
public static MailInfoData GetMailInfoData(Dictionary<string, object> dicData){
int onlyId = int.Parse(dicData[“onlyId”].ToString());
int cfgId = int.Parse(dicData[“mailCfgId”].ToString());
int type = int.Parse(dicData[“mailType”].ToString());
long createTime = long.Parse(dicData[“createTime”].ToString());
long awardTime = long.Parse(dicData[“awardTime”].ToString());
long outDateTime = long.Parse(dicData[“outDateTime”].ToString());
int state = int.Parse(dicData[“state”].ToString());
//参数
List<string> paramList = new List<string>();
if(dicData.ContainsKey(“params”)){
List<object> tempParamList = dicData[“parmas”] as List<object>;
for(int i = 0; i < tempParamList.Count; i++){
paramList.Add(tempParamList[i].ToString());
}
}
//奖励
RewardController rewardController = new RewardController();
rewardController.ParseReward(dicData);
MailInfoData data = new MailInfoData(onlyId, cfgId, type, createTime, awardTime, outDateTime, state);
return data;
}
public override string ToString(){
return string.Format(“MailInfoData: OnlyId={0}, MailCfgId={1}, MailType={2}, ....” );
}
}