定义枚举类型规避写错常量

public class FadeInAndOut : MonoBehaviour

后面继承的 MonoBehaviour是unity中需要赋值在gameobject上需要继承的,如果不需要用,只是写一个类就删除这个继承

单例模式
首先构造函数一定是私有的,这样就可以防止其他函数new新的对象出来,私有一个静态的类出来,静态类默认为空,在空的情况下允许nwe出的新的对象,如果不为空就不允许实例新的对象出来,这就保证了只有一个实例。

/**
 * 
 * 
 *    Title:枚举类型转换字符串
 *
 *    description:
 *    定义枚举类型
 *    定义项目委托
 *    定义系统常量
 *    
 *
 *    Date:
 *
 *    version:
 *
 *    modify Recoder:
 * 
 * 
 **/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Global {
    public class GlobalEnumToStr
    {
        //私有出静态的类
        private static GlobalEnumToStr _Instance;
        //枚举场景类型集合
        private Dictionary<ScenesEnum, string> _DicScenesEnumLib;

        //构造函数需要是私有的这样才能防止new出新的对象
        private GlobalEnumToStr()
        {
            _DicScenesEnumLib = new Dictionary<ScenesEnum, string>();
            _DicScenesEnumLib.Add(ScenesEnum.StartScenes, "1_SceneScenes");
            _DicScenesEnumLib.Add(ScenesEnum.LogonScenes, "2_LogonScenes");
            _DicScenesEnumLib.Add(ScenesEnum.LoadingScenes, "Loading_Scenes");
        }
        //单例模式的应用
        public static GlobalEnumToStr GetInstance()
        {
            if (_Instance == null)
            {
                _Instance = new GlobalEnumToStr();
            }
            return _Instance;
        }
        //得到字符串形式的场景名称
        public string GetStrByEnumScenes(ScenesEnum scenesEnum)
        {
            if(_DicScenesEnumLib!=null&&_DicScenesEnumLib.Count >= 1)
            {
                return _DicScenesEnumLib[scenesEnum];
            }
            else
            {
                Debug.LogWarning(GetType() + "_DicScenesEnumLib为空,DicScenesEnumLib.Count<0,请检查");
                return null;
            }
        }
    }
}

把要用的场景通过枚举类型列举出来,然后通过上面的枚举转换字符串,将枚举类型转换为字符串,这样在调用系统常量的时候就可以通过调用枚举来执行,这样做的好处是如果有单词写错了可以编译器会报错,这样就规避了写错常量的问题

/**
 * 
 * 
 *    Title:公共层:全局参数
 *
 *    description:
 *     
 *     定义整个项目的枚举类型
 *     定义整个项目的系统常量
 *     定义整个项目的委托
 *
 *    Date:
 *
 *    version:
 *
 *    modify Recoder:
 * 
 * 
 **/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Global
{
    public class GLO_paramter 
    {
        // Start is called before the first frame update
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {

        }

    }
    public enum ScenesEnum
    {
        StartScenes,
        LoadingScenes,
        LogonScenes,
        LevelOne,
        LevelTwo,
        BaseScense,

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值