日积月累的小技巧

1,迭代器的使用

 private List<int> _testList = new List<int>(new int[] {1,2,3,4,5});
    void testList()
    {

        // foreach(var item in _testList)
        // {
        //  Debug.Log(item*item);
        // }
        IEnumerator<int> itList = GetEnumeratorTest();
        while(itList.MoveNext())
        {

            Debug.Log("GetEnumeratorTest a " + _testList.Count);
            if(3 == (int)itList.Current)
            {
                Debug.Log(itList.Current);
                _testList.Remove((int)itList.Current);
                 Debug.Log("GetEnumeratorTest c " + _testList.Count);
                 _testList.Add(6);
                  Debug.Log("GetEnumeratorTest d " + _testList.Count);
            }

            Debug.Log("GetEnumeratorTest b" + _testList.Count);
        }
        Debug.Log("_testList.GetEnumerator();");
        IEnumerator it = _testList.GetEnumerator();

        while(it.MoveNext())
        {
             Debug.Log(it.Current);

             Debug.Log("_testList " + _testList.Count);
        }
    }

    public IEnumerator<int> GetEnumeratorTest()
    {
         //此处应该for 不能使用foreach
        for(int item = 0 ;item < _testList.Count;item++)
        {
            yield return _testList[item];
        }


    }

2,使用异或 从数组中取值

// 异或计算  
    void ExclusiveOr()
    {
        int[] arr = {31,32,666,88,31,32,188};
         int count = arr.Length;
         int result = 0;
         for(int i = 0;i < count;i++)
         {
            result ^= arr[i];
         }
         Debug.Log("result = " + result);
    }

3,协程的巧妙

void CoroutinesTest()
    {       
        _coroutinesList.Add(2.0f);
        _coroutinesList.Add(2.0f);
        _coroutinesList.Add(3.0f);
        _coroutinesList.Add(4.0f);
        StartCoroutine(CountSeconds());  
    }
    List<float> _coroutinesList = new List<float>();
    IEnumerator CountSeconds()  
    {  
        float seconds = 0.0f;  

        while(seconds <= 1.0f)  
        {  
            for(int  i = 0; i < _coroutinesList.Count; i++)
            {
                _coroutinesList[i] -= 0.1f;
                Debug.Log("_coroutinesList[i] = " + i + "  " + _coroutinesList[i]);
                if(_coroutinesList[i] <= 0.0f)
                    seconds += 0.25f; 
                yield return 0;
            }



            Debug.Log(seconds +" seconds have passed since the Coroutine started.");  
        }  
    }

4,位运算的用处

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum UIMenuType
{
    None        = 0,
    Title       = 1,                 
    SubTitle    = 1 << 1,             
    Fight       = 1 << 2,
    Upgrade     = 1 << 3,
    Repair      = 1 << 4,
    Max         = 1 << 5,
}

public class MenuTest : MonoBehaviour {

    private UIMenuType mMenuType = UIMenuType.None;
    void Start () {
        mMenuType =  UIMenuType.SubTitle | UIMenuType.Fight ;
        ShowMenu();
        Debug.Log("=====================  =============================");
        mMenuType =  UIMenuType.Upgrade | UIMenuType.Repair ;
        ShowMenu(); 
        Debug.Log("=====================  =============================");



    }

    void ShowMenu()
    {
        uint type = (uint)UIMenuType.Max;
        type >>= 1;
        while((UIMenuType)type != UIMenuType.None)
        {
            switch ((UIMenuType)type & mMenuType)
            {
                case UIMenuType.SubTitle:
                    Debug.Log("SubTitle");
                    break;
                case UIMenuType.Fight:
                     Debug.Log("Fight");
                    break;
                case UIMenuType.Upgrade:
                     Debug.Log("Upgrade");
                    break;
                case UIMenuType.Repair:
                     Debug.Log("Repair");
                    break;
                default:
                    break;
            }
            type >>= 1;
        }
    }
}

5,经典算法 计算二进制中有多少个1
https://www.cnblogs.com/graphics/archive/2010/06/21/1752421.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值