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