C#笔记 使用杂项

来源:互联网 发布:数据之巅涂子沛txt下载 编辑:程序博客网 时间:2024/06/06 23:52

预处理

#define DEBUG // 这个要声明在using关键字之前using System;#if DEBUG#elif DEBUG_IOS#else#endif

继承MonoBehaviour
继承MonoBehaviour的类不能使用new创建,同时也不能使用构造函数。

String

// 从第0个字节找空格的位置int n = str1.IndexOf( ' ', 0 );// 删除第一空格后所有字符str2 = str1.Remove(n);// 替换所有空格为'-'str2 = str1.Replace( ' ', '-' );// 在空格后插入str2 = str1.Insert( n, "test" );// 取空格后6字符str2 = str1.Substring( n+1, 6 );// 已空格为标识符,分解为strs.Length个新字符串char[] chars = { ' ' };string[] strs = str1.Split(chars);

使用bytes

int data1 = 100;float data2 = 0.1f;// 转换成byteint index = 0;byte[] bytes = new bytes[256];byte[] b = BitConverter.GetBytes(data1);b.CopyTo( bytes, index );index += 4;b = BitConverter.GetBytes(data2);b.CopyTo( bytes, index );// 转换回int readdata1 = BitConverter.ToInt32( bytes, 0 );float readdata2 = BitConverter.ToSingle( bytes, 4 );

Awake and Start
Awake 即使不是script不enable也会调用
Start在enable时调用

Value and Reference
Vector3 和 Quaternion是Structs是Value
Transform和GameObject是Classes是Reference

Invoke
Invoke( “functionName”, 3 ); 3秒后调用functionName().
被触发的函数必须是void且没有参数
InvokeRepeating( “functionName”, 3, 1 );3秒后开始,每隔1秒周期调用
CancelInvoke( “functionName” );

Enumeration
改变enum的类型为short
enum Direction : short { North,East,South, West };

Generic

public T GenericMethod<T>( T param ) where T : class

where后面:
class可以指明是个refence type
struct可以指明是个value type
new()可以指明没有参数的构造
MonoBehaviour可以指明是这个类或它的子类
IEnumerbale可以指明实现了借口的

Extension

public static class ExtensionMethods{    public static void ResetTransformation(this Transform trans)    {        trans.position = Vector3.zero;        trans.localRotation = Quaternion.identity;        trans.localScale = new Vector3(1, 1, 1);    }}
0 0