字典排序,string类型字符串提取数字,去掉实例化ObjectGame时带(clone)后缀,Fps显示脚本,以及单例管理脚本以及使用

这里主要是记录几个方法已被后续如果使用先行记录,具体学习方式请参考以下链接:

字符串提取数字:https://www.cnblogs.com/xuliangxing/p/7993267.html

字典排序:https://blog.csdn.net/aming_china/article/details/80310901

功能:根据字典储存时间和图片生成预览图,该图预制体下有一个Button和Text。

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class LoadImage : MonoBehaviour 
{
    //图片预制体
    public GameObject Image;
    //储存时间和图片的字典
    public Dictionary<string, Sprite> programs = new Dictionary<string, Sprite>();//存储保存的方案的时间和截图
    //父物体
    public Transform Parent;

    private List<GameObject> list = new List<GameObject>();
    void Start () 
	{
 
        //AddDate();
        Init();


    }
    /// <summary>
    /// 初始化
    /// </summary>
    private void Init()
    {
        if (programs.Count > 0)
        {
            SortDic(programs);
        }
        else
        {
            Debug.Log("没有数据");
        }
    }

    
    /// <summary>
    /// 添加数据,做测试用
    /// </summary>
    private void AddDate()
    {
        Sprite sprite2 = PathToSprite("image/img2");
        string text2 = "2019/3/7 09:16";
        programs.Add(text2, sprite2);

        Sprite sprite3 = PathToSprite("image/img3");
        string text3 = "2019/3/6 09:20";
        programs.Add(text3, sprite3);

        Sprite sprite4 = PathToSprite("image/img4");
        string text4 = "2019/3/8 09:30";
        programs.Add(text4, sprite4);

        Sprite sprite1 = PathToSprite("image/img1");
        string text1 = "2019/3/10 10:02";
        programs.Add(text1, sprite1);

        
    }
    /// <summary>
    /// 字典根据键值时间排序
    /// </summary>
    private void SortDic(Dictionary<string,Sprite> dic)
    {
        //float.Parse(System.Text.RegularExpressions.Regex.Replace(objDic.Key, @"[^0-9]+", ""))将键值时间只获取数字转成floa格式排序
        //字典根据键值排序objDic.Key,根据值排序则使用objDic.Value
        var dicSort = from objDic in dic orderby float.Parse(System.Text.RegularExpressions.Regex.Replace(objDic.Key, @"[^0-9]+", "")) descending select objDic;

        foreach (KeyValuePair<string, Sprite> kvp in dicSort)
        {
            GameObject Prefab = Instantiate(Image) as GameObject;
            Prefab.transform.GetChild(0).GetComponent<Image>().sprite = kvp.Value;
            Prefab.transform.GetChild(1).GetComponent<Text>().text = kvp.Key;
            Prefab.transform.SetParent(Parent);
            list.Add(Prefab);
        }
    }
    /// <summary>
    /// 将路径图片转化成sprite类型
    /// </summary>
    /// <param name="Path"></param>
    /// <returns></returns>
    public Sprite PathToSprite(string Path)
    {
        Texture2D texture2D = Resources.Load(Path) as Texture2D;
        Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
        return sprite;
    }
}
    /// <summary>
    /// 去掉预制体生成的物体后面带的"(clone)"字符
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public string CloneName(string name)
    {
        try
        {
            string NewName = name.Replace("(Clone)", "");
            return NewName;
        }
        catch (System.Exception)
        {
            return name;
            throw;
        }
        
    }

Fps显示脚本:

using UnityEngine;
using System.Collections;

public class ShowFpsOnGUI : MonoBehaviour
{

    public float fpsMeasuringDelta = 2.0f;
    public int TargetFrame = 30;

    private float timePassed;
    private int m_FrameCount = 0;
    private float m_FPS = 0.0f;

    private void Start()
    {
        timePassed = 0.0f;
        Application.targetFrameRate = TargetFrame;
    }

    private void Update()
    {
        m_FrameCount = m_FrameCount + 1;
        timePassed = timePassed + Time.deltaTime;

        if (timePassed > fpsMeasuringDelta)
        {
            m_FPS = m_FrameCount / timePassed;

            timePassed = 0.0f;
            m_FrameCount = 0;
        }
    }

    private void OnGUI()
    {
        GUIStyle bb = new GUIStyle();
        bb.normal.background = null;
        bb.normal.textColor = new Color(1.0f, 0.5f, 0.0f);
        bb.fontSize = 32;

        //居中显示FPS
        GUI.Label(new Rect(0, 30, 200, 200), "FPS: " + m_FPS, bb);
    }
}

单例脚本:

using UnityEngine;


/// <summary>
/// Singleton class
/// </summary>
/// <typeparam name="T">Type of the singleton</typeparam>
public abstract class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
    /// <summary>
    /// The static reference to the instance
    /// </summary>
    public static T instance { get; protected set; }

    /// <summary>
    /// Gets whether an instance of this singleton exists
    /// </summary>
    public static bool instanceExists
    {
        get { return instance != null; }
    }

    /// <summary>
    /// Awake method to associate singleton with instance
    /// </summary>
    protected virtual void Awake()
    {
        if (instanceExists)
        {
            Destroy(gameObject);
        }
        else
        {
            instance = (T)this;
        }
    }

    /// <summary>
    /// OnDestroy method to clear singleton association
    /// </summary>
    protected virtual void OnDestroy()
    {
        if (instance == this)
        {
            instance = null;
        }
    }
}

使用方式:

需要单例的脚本:public class 类名: Singleton<类名> 继承即可。

比如:

public class SaveMannager : Singleton<SaveMannager> 
{

}

调用则直接调用即可:

 SaveMannager saveMannager;

    void Start () 
	{
        saveMannager = SaveMannager.instance;

    }

非法字符判断:

            Regex regExp = new Regex("[ \\[ \\] \\^ \\-_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]");
            if (regExp.IsMatch(NameInput.text))
            {
                string text = "请勿输入非法字符";
                Tips.GetComponent<TipsShow>().TipTextShow(text, true, Color.red);
            }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值