{"id":8409,"date":"2024-06-10T20:01:03","date_gmt":"2024-06-10T12:01:03","guid":{"rendered":""},"modified":"2024-06-10T20:01:03","modified_gmt":"2024-06-10T12:01:03","slug":"unity3d\u7528\u4ec0\u4e48\u8bed\u8a00_UNITY\u6e38\u620f","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8409.html","title":{"rendered":"unity3d\u7528\u4ec0\u4e48\u8bed\u8a00_UNITY\u6e38\u620f"},"content":{"rendered":"
Unity3D\u63d0\u4f9b\u652f\u6301\u4e09\u79cd\u811a\u672c\u8bed\u8a00\uff0c\u4f46\u662fJscript\u5e76\u975e\u4e00\u6211\u4eec\u6240\u719f\u77e5\u7684\u90a3\u4e2aJscript\uff0c\u6709\u7740\u5f88\u5927\u7684\u4e0d\u540c\uff0c\u6240\u4ee5\u5efa\u8bae\u4f7f\u7528\u4f18\u7f8e\u7684C#\u8bed\u8a00\u6765\u5199\u811a\u672c\uff0c\u4e0b\u9762\u662f\u5fc5\u987b\u638c\u63e1\u7684100\u4e2aC#\u57fa\u672c\u70b9\u3002<\/p>\n
1.C#\u4e2d\u4f7f\u7528\/\/(\u53cc\u659c\u6760)\u6765\u8868\u660e\u672c\u884c\u7684\u5269\u4f59\u90e8\u5206\u4ee3\u8868\u6ce8\u91ca\u3002<\/p>\n
2.C#\u4e2d\u8bed\u53e5\u662f\u6309\u987a\u5e8f\u6267\u884c\u7684\uff0c\u6bcf\u6761\u8bed\u53e5\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002<\/p>\n
3.C#\u4e2d\u7684\u51fd\u6570\u6267\u884c\u4e00\u7cfb\u5217\u8bed\u53e5\u7684\u884c\u4e3a\uff0c\u79f0\u4e3a\u8bed\u53e5\u5757---\u4e00\u5bf9\u5927\u62ec\u53f7\u4e2d\u5305\u542b0\u6761\u6216\u591a\u6761\u8bed\u53e5\u3002<\/p>\n
4.\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u53c2\u6570\u6765\u4ece\u8c03\u7528\u8005\u5904\u63a5\u53d7\u8f93\u5165\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u8fd4\u56de\u7c7b\u578b\u6765\u8f93\u51fa\u6570\u636e\u7ed9\u8c03\u7528\u8005\u3002<\/p>\n
5.Main\u51fd\u6570\u5b9a\u4e49\u4e3a\u6267\u884c\u7a0b\u5e8f\u7684\u9ed8\u8ba4\u5165\u53e3\u70b9\u3002Main\u51fd\u6570\u53ef\u4ee5\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\uff0c\u4e5f\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u7ed9\u6267\u884c\u7a0b\u5e8f\u73af\u5883\u3002Main\u51fd\u6570\u4e5f\u53ef\u4ee5\u4e0d\u5b9a\u4e49\u4efb\u4f55\u53c2\u6570\uff0c\u6216\u8005\u5b9a\u4e49\u4e00\u4e2astring\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570(\u8be5\u53c2\u6570\u5c06\u7531\u4f20\u9012\u7ed9\u6267\u884c\u7a0b\u5e8f\u7684\u53c2\u6570\u586b\u5145)\u3002<\/p>\n
6.Using\u6307\u4ee4\u7528\u4e8e\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n
7.C#\u4e2d\u7684\u6807\u8bc6\u7b26\u4ee5\u5b57\u6bcd\u6216\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u5e76\u4e14\u5927\u5c0f\u5199\u654f\u611f\u3002<\/p>\n
8.\u5173\u952e\u5b57\u662f\u7531\u7f16\u8bd1\u5668\u4fdd\u7559\u7684\u540d\u5b57\uff0c\u4e0d\u80fd\u7528\u4f5c\u6807\u8bc6\u7b26\uff0c\u4e0b\u9762\u662fC#\u7684\u5173\u952e\u5b57\u5217\u8868\uff1a<\/p>\n
<\/p>\n
9.\u5982\u679c\u60f3\u5c06\u5173\u952e\u5b57\u4f5c\u4e3a\u6807\u8bc6\u7b26\uff0c\u90a3\u4e48\u53ef\u4ee5\u52a0\u4e0a\u524d\u7f00@\u4ee5\u4f7f\u5b83\u5408\u6cd5\uff0c\u4f8b\u5982\uff1aclass @class\u3002\u6ce8\u610f@\u7b26\u53f7\u5e76\u4e0d\u6784\u6210\u6807\u8bc6\u7b26\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5@myVariable\u548cmyVariable\u662f\u76f8\u540c\u7684\u3002<\/p>\n
10.\u4e00\u4e9b\u5173\u952e\u5b57\u662f\u4e0a\u4e0b\u6587\u76f8\u5173\u7684\uff0c\u5b83\u4eec\u4e0d\u7528\u4f7f\u7528\u7b26\u53f7@\u5c31\u80fd\u591f\u88ab\u7528\u4f5c\u6807\u8bc6\u7b26\u3002\u8fd9\u4e9b\u5173\u952e\u5b57\u5982\u4e0b\uff1a<\/p>\n
<\/p>\n
11.C#\u4e2d\u6240\u6709\u7684\u503c\u90fd\u662f\u4e00\u4e2a\u7279\u5b9a\u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c\u4e00\u4e2a\u503c\u7684\u542b\u4e49\uff0c\u4ee5\u53ca\u4e00\u4e2a\u53d8\u91cf\u53ef\u4ee5\u62e5\u6709\u7684\u6240\u6709\u53ef\u80fd\u7684\u503c\uff0c\u662f\u7531\u5b83\u7684\u7c7b\u578b\u51b3\u5b9a\u7684\u3002<\/p>\n
12.\u9884\u5b9a\u4e49\u7c7b\u578b(\u4e5f\u53eb\u5185\u5efa\u7c7b\u578b)\u662f\u88ab\u7f16\u8bd1\u5668\u7279\u522b\u652f\u6301\u7684\u7c7b\u578b\uff0c\u5982int\u3001float\u3001bool\u7b49\u3002<\/p>\n
13.\u81ea\u5b9a\u4e49\u7c7b\u578b\u53ef\u4ee5\u5305\u542b\u6210\u5458\u6570\u636e\u548c\u6210\u5458\u51fd\u6570\u3002\u9700\u8981\u4f7f\u7528new\u8fd0\u7b97\u7b26\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c\u5f53new\u8fd0\u7b97\u7b26\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u540e\uff0c\u8be5\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u5c06\u88ab\u8c03\u7528\u3002\u6784\u9020\u51fd\u6570\u7684\u5b9a\u4e49\u7c7b\u4f3c\u4e8e\u666e\u901a\u51fd\u6570\u5b9a\u4e49\uff0c\u9664\u4e86\u51fd\u6570\u540d\u5fc5\u987b\u548c\u7c7b\u540d\u76f8\u540c\u4ee5\u53ca\u53bb\u9664\u4e86\u8fd4\u56de\u7c7b\u578b\u3002<\/p>\n
14.\u5728\u7c7b\u578b\u7684\u5b9e\u4f8b\u4e0a\u64cd\u4f5c\u7684\u6570\u636e\u6210\u5458\u548c\u51fd\u6570\u6210\u5458\u79f0\u4e3a\u5b9e\u4f8b\u6210\u5458\u3002\u800c\u4e0d\u5728\u7c7b\u578b\u7684\u5b9e\u4f8b\u4e0a\uff0c\u4f46\u5728\u7c7b\u578b\u672c\u8eab\u4e0a\u64cd\u4f5c\u7684\u6570\u636e\u6210\u5458\u548c\u51fd\u6570\u6210\u5458\uff0c\u5fc5\u987b\u88ab\u6807\u8bb0\u4e3astatic\u3002<\/p>\n
15.public\u5173\u952e\u5b57\u7528\u4e8e\u663e\u9732\u6210\u5458\u7ed9\u5176\u4ed6\u7c7b\u4f7f\u7528\u3002<\/p>\n
16.C#\u80fd\u591f\u5728\u4e24\u4e2a\u76f8\u517c\u5bb9\u7684\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u8f6c\u6362\u603b\u662f\u6839\u636e\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u503c\u800c\u521b\u5efa\u4e00\u4e2a\u65b0\u503c\u3002\u8f6c\u6362\u53ef\u4ee5\u662f\u9690\u5f0f\u7684\u6216\u8005\u663e\u5f0f\u7684\uff0c\u9690\u5f0f\u7684\u8f6c\u6362\u81ea\u52a8\u53d1\u751f\uff0c\u5982int x = 5; long y = x\u3002\u800c\u663e\u5f0f\u7684\u8f6c\u6362\u9700\u8981\u4e00\u4e2a\u6620\u5c04\uff0c\u5982int x = 5; short y = (short)x\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5f53\u7f16\u8bd1\u5668\u80fd\u591f\u4fdd\u8bc1\u5728\u4e24\u4e2a\u7c7b\u578b\u4e4b\u95f4\u8f6c\u6362\u603b\u662f\u6210\u529f\u7684\uff0c\u4e14\u4e0d\u4e22\u5931\u4efb\u4f55\u4fe1\u606f\uff0c\u90a3\u4e48\u5c31\u5141\u8bb8\u9690\u5f0f\u8f6c\u6362\uff0c\u5426\u5219\u7684\u8bdd\uff0c\u5c31\u5fc5\u987b\u4f7f\u7528\u663e\u5f0f\u8f6c\u6362\u3002<\/p>\n
17.C#\u4e2d\u7684\u7c7b\u578b\u53ef\u4ee5\u5206\u4e3a\u503c\u7c7b\u578b\u548c\u5f15\u7528\u7c7b\u578b\u3002\u503c\u7c7b\u578b\u5305\u62ec\u4e86\u5927\u591a\u6570\u5185\u7f6e\u7c7b\u578b(\u6240\u6709\u7684\u6570\u503c\u7c7b\u578b(int, float\u7b49)\uff0cchar\u7c7b\u578b\uff0cbool\u7c7b\u578b\u4ee5\u53castruct\u4e0eenum\u7c7b\u578b)\uff0c\u5f15\u7528\u7c7b\u578b\u5305\u62ec\u4e86\u6240\u6709\u7684class\uff0carray\uff0cdelegate\u4e0einterface\u7c7b\u578b\u3002<\/p>\n
18.\u4e00\u4e2a\u503c\u7c7b\u578b\u7684\u53d8\u91cf\u6216\u5e38\u91cf\u5305\u542b\u7684\u5185\u5bb9\u4ec5\u4ec5\u662f\u4e00\u4e2a\u503c\uff0c\u5bf9\u4e00\u4e2a\u503c\u7c7b\u578b\u7684\u5b9e\u4f8b\u8fdb\u884c\u8d4b\u503c\u603b\u662f\u4f1a\u62f7\u8d1d\u5b9e\u9645\u6570\u636e\u5230\u8be5\u5b9e\u4f8b\u3002\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\u6216\u5e38\u91cf\u5305\u542b\u7684\u5185\u5bb9\u662f\u5bf9\u4e00\u4e2a\u62e5\u6709\u5b9e\u9645\u6570\u636e\u7684\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u5bf9\u4e00\u4e2a\u5f15\u7528\u7c7b\u578b\u7684\u5b9e\u4f8b\u8fdb\u884c\u8d4b\u503c\u603b\u662f\u4f1a\u62f7\u8d1d\u5f15\u7528\uff0c\u800c\u4e0d\u4f1a\u62f7\u8d1d\u62e5\u6709\u5b9e\u9645\u6570\u636e\u7684\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u8fd9\u5c31\u5141\u8bb8\u591a\u4e2a\u53d8\u91cf\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n
19.\u4e00\u4e2a\u5f15\u7528\u7c7b\u578b\u7684\u5b9e\u4f8b\u53ef\u4ee5\u88ab\u8bbe\u7f6e\u4e3anull\uff0c\u4ee5\u8868\u660e\u6ca1\u6709\u5f15\u7528\u4efb\u4f55\u5bf9\u8c61\uff0c\u4f7f\u7528\u4e00\u4e2a\u4e3anull\u7684\u5b9e\u4f8b\u7684\u6210\u5458\u5c06\u4ea7\u751f\u4e00\u4e2a\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u4e00\u4e2a\u503c\u7c7b\u578b\u7684\u5b9e\u4f8b\u4e0d\u80fd\u88ab\u8bbe\u7f6e\u4e3anull\u3002<\/p>\n
20.C#\u4e2d\u7684\u9884\u5b9a\u4e49\u7c7b\u578b\u5206\u7c7b\uff1a<\/p>\n
<\/p>\n
21.C#\u9884\u5b9a\u4e49\u7684\u6570\u5b57\u7c7b\u578b\u5982\u4e0b\uff1a<\/p>\n
<\/p>\n
22.\u6574\u578b\u53ef\u4ee5\u4f7f\u752810\u8fdb\u5236\u621616\u8fdb\u5236\u6807\u8bb0\u6cd5\uff0c16\u8fdb\u5236\u6807\u8bb0\u6cd5\u9700\u8981\u52a00x\u524d\u7f00\uff0c\u59820x7F\u3002\u5b9e\u6570\u53ef\u4ee5\u752810\u8fdb\u5236\u6216\u6307\u6570\u6807\u8bb0\u6cd5\uff0c\u59821e06\u3002<\/p>\n
23.\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\u5c06\u4e22\u5f03\u5c0f\u6570\u90e8\u5206\uff0c\u5982\u679c\u60f3\u8981\u8fdb\u884c\u56db\u820d\u4e94\u5165\u7684\u8f6c\u6362\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528System.Convert\u7c7b\u3002<\/p>\n
24.\u9700\u8981\u6ce8\u610f\u4ece1\u4e2a\u5f88\u5927\u7684\u6574\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u65f6\u53ef\u80fd\u4f1a\u4e22\u5931\u7cbe\u5ea6\uff0c\u5982int x = ; float f = x; \/\/ \u6b64\u65f6f = \u3002<\/p>\n
25.\u7b97\u6570\u8fd0\u7b97\u7b26(+,-,*,\/,%)\u53ef\u4ee5\u7528\u4e8e\u6240\u6709\u7684\u6570\u503c\u7c7b\u578b(\u9664\u4e868\u4f4d\u548c16\u4f4d\u7684\u6574\u6570\u7c7b\u578b)\u3002<\/p>\n
26.\u81ea\u589e\uff0c\u81ea\u51cf\u8fd0\u7b97\u7b26\u7528\u4e8e\u4f7f\u6570\u503c\u7c7b\u578b\u7684\u53d8\u91cf\u52a01\u6216\u51cf1\u3002\u8fd0\u7b97\u7b26\u53ef\u4ee5\u653e\u5728\u53d8\u91cf\u524d(++x)\u6216\u8005\u53d8\u91cf\u540e(x++)\uff0c\u53d6\u51b3\u4e8e\u60f3\u5728\u8868\u8fbe\u5f0f\u8ba1\u7b97\u4e4b\u524d\u8fd8\u662f\u4e4b\u540e\u66f4\u65b0\u53d8\u91cf\u7684\u503c\u3002<\/p>\n
27.\u6574\u6570\u9664\u6cd5\u603b\u662f\u4f1a\u4e22\u5f03\u4f59\u6570\u3002\u5982\u679c\u9664\u4ee5\u4e00\u4e2a\u503c\u4e3a0\u7684\u53d8\u91cf\u5c06\u4ea7\u751f\u4e00\u4e2a\u8fd0\u884c\u65f6\u9519\u8bef(DivideByZeroException)\uff0c\u9664\u4ee5\u5b57\u9762\u503c0\u5c06\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002<\/p>\n
28.\u5728\u8fd0\u884c\u65f6\uff0c\u6574\u6570\u7c7b\u578b\u8fdb\u884c\u7b97\u672f\u8fd0\u7b97\u53ef\u80fd\u4f1a\u9020\u6210\u6ea2\u51fa\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0d\u4f1a\u6709\u5f02\u5e38\u88ab\u629b\u51fa\u3002C#\u6587\u6863\u8868\u660e\u5bf9\u4e8e\u6ea2\u51fa\u7ed3\u679c\u662f\u4e0d\u53ef\u9884\u6599\u7684\uff0c\u4f46\u662fCommon Language Runtime(CLR)\u603b\u662f\u4ea7\u751f\u4e00\u4e2awraparound\u884c\u4e3a\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5c06\u6700\u5c0f\u7684int\u503c\u51cf1\u7684\u7ed3\u679c\u662f\u6700\u5927\u7684int\u503c\uff0c\u5982int x = int.MinValue; x--; \/\/ \u6b64\u65f6x = int.MaxValue\u3002<\/p>\n
29.checked\u8fd0\u7b97\u7b26\u901a\u77e5\u8fd0\u884c\u65f6\u5f53\u6ea2\u51fa\u65f6\u629b\u51fa\u4e00\u4e2aOverflowException\u5f02\u5e38\uff0cchecked\u8fd0\u7b97\u7b26\u53ef\u4ee5\u7528\u4e8e++, --, -(\u4e00\u5143), +, -, *, \/\u4ee5\u53ca\u6574\u6570\u7c7b\u578b\u4e4b\u95f4\u7684\u663e\u793a\u8f6c\u6362\u3002checked\u8fd0\u7b97\u7b26\u53ef\u4ee5\u7528\u4e8e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6216\u8005\u4e00\u4e2a\u8bed\u53e5\u5757\uff0c\u5982\uff1a<\/p>\n