Unity经验之谈
阅读原文时间:2023年07月14日阅读:1

1、全屏与非全屏之间的切换

   if (Input.GetMouseButtonDown(1))  
    {  
        Screen.fullScreen = !Screen.fullScreen;  
    }

2、Camera适应所有的分辨率

    void Start () {
Camera.main.aspect = 16 / 10f;
 }

3、字符串分割时,一定要注意空格,空格在日志中不好看出来。

        string a = "a,b,c ";  
        string\[\] aa = a.Split(',');  
        string value=aa\[2\];//最后会得到"c "  
        //最好的方式是多加一个","  
        string b = "a,b,c,";  
        string\[\] bb = b.Split(',');  
        string value1 = bb\[2\];//最后会得到"c"

4、win10和win7 进程名称不同

win10 的进程名是PlayerSettings里的Product Name,

而比较坑的是在win7系统下,同一个程序,进程名居然是.exe的名字,例如你的unity 打包的exe名字叫做 Test.exe,

而Product Name名字叫 MyTest,则win10的进程名为 MyTest,而win7的进程名却TM是 Test ,坑死。

5、unity在调用外部的库时(如:dll, jar,so),传递的参数一定要注意C#的垃圾回收机制。

如果传入外部的参数,如果被垃圾回收掉GC.Collect(),可能会导致程序崩溃,报非法访问内存的错误。

解决的办法就是将参数设置为静态的全局变量,因为 static 是不会被GC掉的。

如:在PC端,会报如下错误

xxx by xxx [version: Unity 2018.2.15f1_65e0713a5949]

mono-2.0-bdwgc.dll caused an Access Violation (0xc0000005)
in module mono-2.0-bdwgc.dll at 0023:73880af6.

Error occurred at 2019-03-01_160714.
C:\Program Files (x86)\xxx.exe, run by xxx.

14% physical memory in use.
8192 MB physical memory [0 MB free].
396 MB process peak paging file [289 MB used].
378 MB process peak working set [273 MB used].
System Commit Total/Limit/Peak: 1275MB/1216MB/1610MB
System Physical Total/Available: 0MB/2921MB
System Process Count: 36
System Thread Count: 534
System Handle Count: 13328
Disk space data for 'C:\Users\xxx\Crash_2019-03-01_080711713\': 39757352960 bytes free of 64422408192 total.

Read from location 3A226578 caused an access violation.

Context:
EDI: 0x0a75dfe8 ESI: 0x00000000 EAX: 0x0a448760
EBX: 0x0a75dfe0 ECX: 0x3a226572 EDX: 0xff52f000
EIP: 0x73880af6 EBP: 0x00a0f7a8 SegCs: 0x00000023
EFlags: 0x00010206 ESP: 0x00a0f7a8 SegSs: 0x0000002b

Bytes at CS:EIP:
0f be 41 06 83 e8 0f 83 f8 0f 77 29 0f b6 80 ac

Stack Trace of Crashed Thread 4044:
0x73880AF6 (mono-2.0-bdwgc) mono_type_to_unmanaged
0x73880A28 (mono-2.0-bdwgc) mono_type_to_unmanaged
0x7381E91C (mono-2.0-bdwgc) mono_conc_hashtable_foreach_steal
0x73879D5D (mono-2.0-bdwgc) mono_metadata_blob_heap
0x7385B21A (mono-2.0-bdwgc) mono_image_close
0x7382866E (mono-2.0-bdwgc) mono_assembly_close
0x73825AC5 (mono-2.0-bdwgc) mono_domain_free
0x738FCA51 (mono-2.0-bdwgc) mono_unity_unlock_dynamic_function_access_tables64
ERROR: SymGetSymFromAddr64, GetLastError: '试图访问无效的地址。
' (Address: 7465D982)
0x7465D982 (UnityPlayer) (function-name not available)
ERROR: SymGetSymFromAddr64, GetLastError: '试图访问无效的地址。
' (Address: 7458BCA5)
0x7458BCA5 (UnityPlayer) (function-name not available)
ERROR: SymGetSymFromAddr64, GetLastError: '试图访问无效的地址。
' (Address: 74B22D09)
0x74B22D09 (UnityPlayer) (function-name not available)
ERROR: SymGetSymFromAddr64, GetLastError: '试图访问无效的地址。
' (Address: 74B220F8)
0x74B220F8 (UnityPlayer) (function-name not available)
ERROR: SymGetSymFromAddr64, GetLastError: '试图访问无效的地址。
' (Address: 74B24531)
0x74B24531 (UnityPlayer) (function-name not available)
0x74B24705 (UnityPlayer) UnityMain
ERROR: SymGetSymFromAddr64, GetLastError: '试图访问无效的地址。
' (Address: 00F51015)
0x00F51015 (XESAIMandarin) (function-name not available)
ERROR: SymGetSymFromAddr64, GetLastError: '试图访问无效的地址。
' (Address: 00F511D0)
0x00F511D0 (XESAIMandarin) (function-name not available)
0x766F8543 (KERNEL32) BaseThreadInitThunk
0x77DAAC69 (ntdll) RtlInitializeExceptionChain
0x77DAAC3C (ntdll) RtlInitializeExceptionChain

Stacks for Running Threads:

Call Stack for Thread 3304:
0x77D90DA8 (ntdll) NtWaitForSingleObject
ERROR: SymGetSymFromAddr64, GetLastError: '试图访问无效的地址。
' (Address: 745DCB19)
0x745DCB19 (UnityPlayer) (function-name not available)
0x74B4A5D8 (UnityPlayer) UnityMain
0x74B51A8A (UnityPlayer) UnityMain
0x74B51B78 (UnityPlayer) UnityMain
ERROR: SymGetSymFromAddr64, GetLastError: '试图访问无效的地址。
' (Address: 745DC06D)
0x745DC06D (UnityPlayer) (function-name not available)
0x766F8543 (KERNEL32) BaseThreadInitThunk
0x77DAAC69 (ntdll) RtlInitializeExceptionChain
0x77DAAC3C (ntdll) RtlInitializeExceptionChain

Call Stack for Thread 3932:
0x77D90DA8 (ntdll) NtWaitForSingleObject
0x75AE10B4 (KERNELBASE) WaitForSingleObject
0x72B72398 (IMSCCORE) CreateUDicProxy
0x72B6EA6A (IMSCCORE) CreateUDicProxy
0x766F8543 (KERNEL32) BaseThreadInitThunk
0x77DAAC69 (ntdll) RtlInitializeExceptionChain
0x77DAAC3C (ntdll) RtlInitializeExceptionChain

Call Stack for Thread 2240:
0x77D92828 (ntdll) NtWaitForWorkViaWorkerFactory
0x766F8543 (KERNEL32) BaseThreadInitThunk
0x77DAAC69 (ntdll) RtlInitializeExceptionChain
0x77DAAC3C (ntdll) RtlInitializeExceptionChain

Call Stack for Thread 3416:
0x77D92828 (ntdll) NtWaitForWorkViaWorkerFactory
0x766F8543 (KERNEL32) BaseThreadInitThunk
0x77DAAC69 (ntdll) RtlInitializeExceptionChain
0x77DAAC3C (ntdll) RtlInitializeExceptionChain

Call Stack for Thread 1992:
0x77D90DF8 (ntdll) NtRemoveIoCompletion
0x766F8543 (KERNEL32) BaseThreadInitThunk
0x77DAAC69 (ntdll) RtlInitializeExceptionChain
0x77DAAC3C (ntdll) RtlInitializeExceptionChain

Call Stack for Thread 1372:
0x77D92828 (ntdll) NtWaitForWorkViaWorkerFactory
0x766F8543 (KERNEL32) BaseThreadInitThunk
0x77DAAC69 (ntdll) RtlInitializeExceptionChain
0x77DAAC3C (ntdll) RtlInitializeExceptionChain

Stack Memory [0x00A0F7A8-0x00A10000]:
0x00a0f7a8: 00a0f7c8 73880a28 3a226572 081c7b20 ….(..sre": {..
0x00a0f7b8: 00000003 0a6c56b0 0816d418 081c7b20 …..Vl….. {..
0x00a0f7c8: 00a0f7ec 7381e91c 0a75a250 0a75a250 …….sP.u.P.u.
0x00a0f7d8: 00a0f80c 081c7b20 0a70f310 0816d4d8 …. {….p…..
0x00a0f7e8: 0a70f384 00a0f81c 73879d5d 0a720108 ..p…..]..s..r.
0x00a0f7f8: 738809f0 00a0f80c 081c7b20 00000000 …s…. {……
0x00a0f808: 77d9f4d0 081c7b20 0ab8ca88 081c7b20 …w {…… {..
0x00a0f818: 00000000 00a0f838 7385b21a 081c7b20 ….8……s {..
0x00a0f828: 081a6568 080d0ff0 080d0ff0 ffffffff he…………..
0x00a0f838: 00a0f848 7382866e 081c7b20 02ab2e70 H…n..s {..p…
0x00a0f848: 00a0f888 73825ac5 080d0ff0 00000040 …..Z.s….@…
0x00a0f858: 00000001 73ad8714 00d0ae08 02ab2e70 …….s….p…
0x00a0f868: 0a4ee537 080d0ff0 00000001 02ab2e70 7.N………p…
0x00a0f878: 00d2b0e0 762c7462 00d01668 00000000 ….bt,vh…….
0x00a0f888: 00a0f8b0 738fca51 02ab2e70 00000001 ….Q..sp…….
0x00a0f898: 00d2b0e0 00d367a8 00d2b0e0 00d2b0e0 …..g……….
0x00a0f8a8: 762d5dbc 762b7f50 00a0f8cc 7465d982 .]-vP.+v……et
0x00a0f8b8: 02ab2e70 00a0f8c8 00000000 762b7f50 p………..P.+v
0x00a0f8c8: 02ab2e70 00a0f8e0 7458bca5 00000000 p………Xt….
0x00a0f8d8: 00000000 00a0f8fc 00a0f8fc 74b22d09 ………….-.t
0x00a0f8e8: 00000000 74b220c4 00a0f910 00000000 ….. .t……..
0x00a0f8f8: 00000000 00a0f930 74b220f8 00000001 ….0…. .t….
0x00a0f908: 00000001 00000000 00040d0c 00000200 …………….
0x00a0f918: 00000000 018e0274 001c8ad1 000003b2 ….t………..
0x00a0f928: 000001f5 00000584 00a0f9f4 74b24531 …………1E.t
0x00a0f938: ff529000 00f6327c 00000000 00ce0100 ..R.|2……….
0x00a0f948: 00a0f8c8 00000000 00a0fa64 77e335f2 ……..d….5.w
0x00a0f958: c57085d1 02cb3c70 00000051 77da32ff ..p.p<..Q….2.w
0x00a0f968: 00000800 00000808 00000051 00000044 ……..Q…D…
0x00a0f978: 00000000 65726170 5748746e 0000444e ….parentHWND..
0x00a0f988: 00000000 0000000a 00000044 00000000 ……..D…….
0x00a0f998: 006c6174 206e776f 646e6556 0000726f tal.own Vendor..
0x00a0f9a8: 00000003 00000044 00000000 00000000 ….D………..
0x00a0f9b8: 65646968 646e6957 0000776f 77e335f2 hideWindow…5.w
0x00a0f9c8: 0000000a 00000044 00000500 00000000 ….D………..
0x00a0f9d8: 6f747561 6974616d 772d6e6f 00006265 automation-web..
0x00a0f9e8: 0000000e 00000044 00a0f9f4 00a0fa10 ….D………..
0x00a0f9f8: 74b24705 00f50000 00000000 00ce0b5a .G.t……..Z…
0x00a0fa08: 00000001 00f6327c 00a0fa28 00f51015 ….|2..(…….
0x00a0fa18: 00f50000 00000000 00ce0b5a 00000001 ……..Z…….
0x00a0fa28: 00a0fa74 00f511d0 00f50000 00000000 t……………
0x00a0fa38: 00ce0b5a 00000001 c51c4769 00000000 Z…….iG……
0x00a0fa48: 00000000 ff529000 fffff800 c0000005 ……R………
0x00a0fa58: 00000000 00a0fa40 00a0f1d4 00a0fab4 ….@………..
0x00a0fa68: 00f51ba0 c54ab0bd 00000000 00a0fa80 ……J………
0x00a0fa78: 766f8543 ff529000 00a0fac4 77daac69 C.ov..R…..i..w
0x00a0fa88: ff529000 b209a8cd 00000000 00000000 ..R………….
0x00a0fa98: ff529000 c0000005 fffff803 00a0f1bc ..R………….
0x00a0faa8: 75b705d2 00a0fa8c 00a0f1bc 00a0fad4 …u…………
0x00a0fab8: 77e335f2 c570ba09 00000000 00a0fadc .5.w..p………
0x00a0fac8: 77daac3c 00f51242 ff529000 ffffffff <..wB…..R…..
0x00a0fad8: 77e21d86 00000000 00000000 00f51242 …w……..B…
0x00a0fae8: ff529000 00000000 00000000 00000000 ..R………….
0x00a0faf8: 00000000 00000000 00000000 00000000 …………….
0x00a0fb08: 00000000 00000000 00000000 00000000 …………….
0x00a0fb18: 00000000 00000000 00000000 00000000 …………….
0x00a0fb28: 00000000 00000000 00000000 00000000 …………….
0x00a0fb38: 00000000 00000000 00000000 00000000 …………….
0x00a0fb48: 00000000 00000000 00000000 00000000 …………….
0x00a0fb58: 00000000 00000000 00000000 00000000 …………….
0x00a0fb68: 00000000 00000000 00000000 00000000 …………….
0x00a0fb78: 00000000 00000000 00000000 00000000 …………….
0x00a0fb88: 00000000 00000000 00000000 00000000 …………….
0x00a0fb98: 00000000 00000000 00000000 00000000 …………….
0x00a0fba8: 00000000 00000000 00000000 00000000 …………….
0x00a0fbb8: 00000000 00000000 00000000 00000000 …………….
0x00a0fbc8: 00000000 00000000 00000000 00000000 …………….
0x00a0fbd8: 00000000 00000000 00000000 00000000 …………….
0x00a0fbe8: 00000000 00000000 00000000 00000000 …………….
0x00a0fbf8: 00000000 00000000 00000000 00000000 …………….
0x00a0fc08: 00000000 00000000 00000000 00000000 …………….
0x00a0fc18: 00000000 00000000 00000000 00000000 …………….
0x00a0fc28: 00000000 00000000 00000000 00000000 …………….
0x00a0fc38: 00000000 00000000 00000000 00000000 …………….
0x00a0fc48: 00000000 00000000 00000000 00000000 …………….
0x00a0fc58: 00000000 00000000 00000000 00000000 …………….
0x00a0fc68: 00000000 00000000 00000000 00000000 …………….
0x00a0fc78: 00000000 00000000 00000000 00000000 …………….
0x00a0fc88: 00000000 00000000 00000000 00000000 …………….
0x00a0fc98: 00000000 00000000 00000000 00000000 …………….
0x00a0fca8: 00000000 00000000 00000000 00000000 …………….
0x00a0fcb8: 00000000 00000000 00000000 00000000 …………….
0x00a0fcc8: 00000000 00000000 00000000 00000000 …………….
0x00a0fcd8: 00000000 00000000 00000000 00000000 …………….
0x00a0fce8: 00000000 00000000 00000000 00000000 …………….
0x00a0fcf8: 00000000 00000000 00000000 00000000 …………….
0x00a0fd08: 00000000 00000000 00000000 00000000 …………….
0x00a0fd18: 00000000 00000000 00000000 00000000 …………….
0x00a0fd28: 00000000 00000000 00000000 00000000 …………….
0x00a0fd38: 00000000 00000000 00000000 00000000 …………….
0x00a0fd48: 00000000 00000000 00000000 00000000 …………….
0x00a0fd58: 00000000 00000000 00000000 00000000 …………….
0x00a0fd68: 00000000 00000000 00000000 00000000 …………….
0x00a0fd78: 00000000 00000000 00000000 00000000 …………….
0x00a0fd88: 00000000 00000000 00000000 00000000 …………….
0x00a0fd98: 00000000 00000000 00000000 00000000 …………….
0x00a0fda8: 00000000 00000000 00000000 00000000 …………….
0x00a0fdb8: 00000000 00000000 00000000 00000000 …………….
0x00a0fdc8: 00000000 00000000 00000000 00000000 …………….
0x00a0fdd8: 00000000 00000000 00000000 00000000 …………….
0x00a0fde8: 00000000 00000000 00000000 00000000 …………….
0x00a0fdf8: 00000000 00000000 00000000 00000000 …………….
0x00a0fe08: 00000000 00000000 00000000 00000000 …………….
0x00a0fe18: 00000000 00000000 00000000 00000000 …………….
0x00a0fe28: 00000000 00000000 00000000 00000000 …………….
0x00a0fe38: 00000000 00000000 00000000 00000000 …………….
0x00a0fe48: 00000000 00000000 00000000 00000000 …………….
0x00a0fe58: 00000000 00000000 00000000 00000000 …………….
0x00a0fe68: 00000000 00000000 00000000 00000000 …………….
0x00a0fe78: 00000000 00000000 00000000 00000000 …………….
0x00a0fe88: 00000000 00000000 00000000 00000000 …………….
0x00a0fe98: 00000000 00000000 00000000 00000000 …………….
0x00a0fea8: 00000000 00000000 00000000 00000000 …………….
0x00a0feb8: 00000000 00000000 00000000 00000000 …………….
0x00a0fec8: 00000000 00000000 00000000 00000000 …………….
0x00a0fed8: 00000000 00000000 00000000 00000000 …………….
0x00a0fee8: 00000000 00000000 00000000 00000000 …………….
0x00a0fef8: 00000000 00000000 00000000 00000000 …………….
0x00a0ff08: 00000000 00000000 00000000 00000000 …………….
0x00a0ff18: 00000000 00000000 00000000 00000000 …………….
0x00a0ff28: 00000000 00000000 00000000 00000000 …………….
0x00a0ff38: 00000000 00000000 00000000 00000000 …………….
0x00a0ff48: 00000000 00000000 00000000 00000000 …………….
0x00a0ff58: 00000000 00000000 00000000 00000000 …………….
0x00a0ff68: 00000000 00000000 00000000 00000000 …………….
0x00a0ff78: 00000000 00000000 00000000 00000000 …………….
0x00a0ff88: 00000000 00000000 00000000 00000000 …………….
0x00a0ff98: 00000000 00000000 00000000 00000000 …………….
0x00a0ffa8: 00000000 00000000 00000000 00000000 …………….
0x00a0ffb8: 00000000 00000000 00000000 00000000 …………….
0x00a0ffc8: 00000000 00000000 00000000 00000000 …………….
0x00a0ffd8: 00000000 00000000 00000000 00000000 …………….
0x00a0ffe8: 00000000 00000000 00000000 00000000 …………….
0x00a0fff8: 00000000 00000000 ……..

Module 1
C:\Program Files (x86)\ѧ��˼��У\AI��ʦ��ͨ����ѧ\xxx.exe
Image Base: 0x00f50000 Image Size: 0x000a1000
File Size: 637952 File Time: 2019-03-01_111200
Version:
Company:
Product:
FileDesc:
FileVer: 2018.2.15.57457
ProdVer: 2018.2.15.57457

Module 2
C:\Windows\SYSTEM32\MSVCR120.dll
Image Base: 0x72590000 Image Size: 0x000ee000
File Size: 970912 File Time: 2013-10-05_023822
Version:
Company: Microsoft Corporation
Product: Microsoft® Visual Studio® 2013
FileDesc: Microsoft® C Runtime Library
FileVer: 12.0.21005.1
ProdVer: 12.0.21005.1

Crash Report configuration:
* App Name: XESAIMandarin
* App Version: Unity 2018.2.15f1_65e0713a5949
* Mono DLL: C:\Program Files (x86)\MonoBleedingEdge\EmbedRuntime\mono-2.0-bdwgc.dll
* Bug Reporter App Path:
* Crash Report Path: C:\Users\CUIZON~1\AppData\Local\Temp\Crashes
* Is Editor: false

Crash Report metadata:

Additional report files:
* "C:\Users\cuizongyao\AppData\LocalLow\output_log.txt" (Output log file)

== [end of error.log] ==

6、unity动画中的SetTrigger方法只有用过以后才置回,如果你将一个动画片段设置SetTrigger而一直没有走到该动画,则SetTrigger的状态会一直为ture,直到播放到该动画片段,SetTrigger才置回原来的状态即false。

7、快速的将sting[ ]或者List转为sting,比for循环快多了

void Awake()
{
List list = new List();
list.Add("1");
list.Add("2");
list.Add("3");

    string value = string.Join("-",list);  
    Debug.Log(value);  
}

输出:

8、Unity 中3D物体添加button响应,在Camera上添加Physics Raycaster即可