Unity开发中注意

来源:互联网 发布:矩阵不可逆说明什么 编辑:程序博客网 时间:2024/05/05 00:58


由于Unity的部分机制具有特殊性,在此列出需要注意的要点。

1.1.  内置方法

Unity提供的固定方法,如StartUpdateOnMouseEnter等,除非有特殊的需要,否则请使用protected访问修饰符以保证良好的封装性。必要时可以使用virtual关键字供子类重写。

1.2.  可选参数

public void MethodWithOptionalParameter(int a = 1)

{

}

继承MonoBehaviour类,不可以声明带可选参数的方法,否则会导致脚本无法附加到游戏对象上。如果为一个已经附加到游戏对象上的脚本增加可选参数方法,同样会导致报无法加载脚本的警告(Can’t add script)。由于报错信息和错误原因不一致,是Unity一个相当莫名的BUG,必须要注意。

1.3.  AOT

有些操作系统不支持动态编译,比如大部分的嵌入式系统以及苹果的IOS。这意味着所有的托管代码都必须使用Full AOT在打包前全部编译成本机代码,因此使用某些泛型机制时会导致抛出如下异常:

ExecutionEngineException: Attempting to JIT compile method….while running with --aot-only.

为考虑到良好的跨平台性,请避免:

  1. 使用带泛型的虚方法;

  2. 在泛型类中使用P/Invoke

  3. 使用System.Reflection.Emit(其他反射机制可用);

参考文档:

http://docs.xamarin.com/guides/ios/advanced_topics/limitations

http://www.mono-project.com/AOT


Unity中使用C#定时器会报错

CompareBaseObjectsInternal can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.


untiy异常报错,可查询C:\Documents and Settings\用户名\Local Settings\Application Data\Unity\Editor


Unity中的Path对应各平台中的Path :



IOS:
Application.dataPath :                      Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data
Application.streamingAssetsPath :   Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw
Application.persistentDataPath :      Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents
Application.temporaryCachePath :   Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches



//**********

CompanyName: DefaultCompanyjqd


ProductName:TestUnityPath


Bundle identifier: com.Company.ProductNamejqd


Android:
Application.dataPath :                         /data/app/xxx.xxx.xxx.apk

/data/app/com.Company.ProductNamejqd-1.apk
Application.streamingAssetsPath :      jar:file:///data/app/xxx.xxx.xxx.apk/!/assets

jar:file:///data/app/com.Company.ProductNamejqd-1.apk!/assets
Application.persistentDataPath :         /data/data/xxx.xxx.xxx/files

/data/data/com.Company.ProductNamejqd/files
Application.temporaryCachePath :      /data/data/xxx.xxx.xxx/cache

/data/data/com.Company.ProductNamejqd/cache


Windows:
Application.dataPath :                         /Assets

自己工程/Assets
Application.streamingAssetsPath :      /Assets/StreamingAssets

自己工程/Assets/StreamingAssets
Application.persistentDataPath :         C:/Users/xxxx/AppData/LocalLow/CompanyName/ProductName

C:/Users/XXXX/AppData/LocalLow/DefaultCompany/TestUnityPath
Application.temporaryCachePath :      C:/Users/xxxx/AppData/Local/Temp/CompanyName/ProductName

C:/Users/xxxx/AppData/Local/Temp/DefaultCompany/TestUnityPath

//**********

//****


CompanyName: DefaultCompanyjqd


ProductName:四个中文字


Bundle identifier: com.xxxxxxx.xxxx.xxxx

windows:

assetSteamFilePath: 自己工程文件夹路径/Assets/StreamingAssets

persistentDataPath: C:/Users/电脑用户名/AppData/LocalLow/DefaultCompanyjqd/____________

dataPath: 自己工程文件夹路径/Assets

temporaryCachePath: C:/Users/xxxx/AppData/Local/Temp/DefaultCompanyjqd/____________

android:

assetSteamFilePath: jar:file:///data/app/com.xxxxxxx.xxxx.xxxx-1.apk!/assets(都加了 -1)

persistentDataPath: /storage/emulated/0/Android/data/com.xxxxxxx.xxxx.xxxx/files

dataPath: /data/app/com.xxxxxxx.xxxx.xxxx-1.apk

temporaryCachePath: /storage/emulated/0/Android/data/com.xxxxxxx.xxxx.xxxx/cache


华为 H30-U10 persistent 存储在了 .../Android/bundle indentifier的名字下的文件夹了

//



Mac:
Application.dataPath :                         /Assets
Application.streamingAssetsPath :      /Assets/StreamingAssets
Application.persistentDataPath :         /Users/xxxx/Library/Caches/CompanyName/Product Name
Application.temporaryCachePath :     /var/folders/57/6b4_9w8113x2fsmzx_yhrhvh0000gn/T/CompanyName/Product Name



Windows Web Player:

Application.dataPath :             file:///D:/MyGame/WebPlayer (即导包后保存的文件夹,html文件所在文件夹)
Application.streamingAssetsPath :
Application.persistentDataPath :
Application.temporaryCachePath :








0 0