當(dāng)對一個(gè)父GameObject進(jìn)行無效設(shè)置后,它的子類gameobject也會(huì)無效,但是并沒有改變子類的狀態(tài),也就是說你沒有辦法使用它自身的屬性activeSelf,判斷一個(gè)子gameobject是否是激活狀態(tài),要使用activeInHierarchy。如果要改變子類的狀態(tài),使用DeactivateChildren
使用transform的一些建議
1,最好把它的父transforn的位置設(shè)置為(0,0,0)這樣對于它來說本地坐標(biāo)和世界坐標(biāo)是一樣的
2,粒子系統(tǒng)的縮放不受transform的影響,需要去設(shè)置粒子發(fā)射器
3,Rigidbody的縮放也不受transform影響,需要在Rigidbody組件上面設(shè)置
4,修改父類的坐標(biāo)會(huì)影響子類的本坐標(biāo)
旋轉(zhuǎn)的正確使用方法
錯(cuò)誤一
// rotation scripting mistake #1
// the mistake here is that we are modifying the x value of a quaternion
// this value does not represent an angle, and will not produce desired results
void Update () {
var rot = transform.rotation;
rot.x += Time.deltaTime * 10;
transform.rotation = rot;
}
錯(cuò)誤二
// rotation scripting mistake #2
// the mistake here is that we are reading, modifying then writing the Euler
// values from a quaternion. Because these values calculated from a Quaternion,
// each new rotation may return very different Euler angles, which may suffer from gimbal lock.
void Update () {
var angles = transform.rotation.eulerAngles;
angles.x += Time.deltaTime * 10;
transform.rotation = Quaternion.Euler(angles);
}
正確的方法
// rotation scripting with Euler angles correctly.
// here we store our Euler angle in a class variable, and only use it to
// apply it as a Euler angle, but we never rely on reading the Euler back.
float x;
void Update () {
x += Time.deltaTime * 10;
transform.rotation = Quaternion.Euler(x,0,0);
}
unity的dll路徑
mac:Applications/Unity/Unity.app/Contents/Frameworks/Managed/
windows:C:\Program Files\Unity\Editor\Data\Managed
加載AssetBundles的四種方式
1,AssetBundle.LoadFromMemoryAsync
IEnumerator LoadFromMemoryAsync(string path)
{
AssetBundleCreateRequest createRequest = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
yield return createRequest;
AssetBundle bundle = createRequest.assetBundle;
var prefab = bundle.LoadAsset.("MyObject");
Instantiate(prefab);
}
這種方式是異步加載一組包含AssetBundle 數(shù)據(jù)的byte數(shù)組到內(nèi)存中,可以添加CRC校驗(yàn),如果使用了LZMA壓縮,會(huì)自動(dòng)解壓
2,AssetBundle.LoadFromFile
public class LoadFromFileExample extends MonoBehaviour {
function Start() {
var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));
if (myLoadedAssetBundle == null) {
Debug.Log("Failed to load AssetBundle!");
return;
}
var prefab = myLoadedAssetBundle.LoadAsset.("MyObject");
Instantiate(prefab);
}
}
Note: On Android devices with Unity 5.3 or older, this API will fail when trying to load AssetBundles from the Streaming Assets path. This is because the contents of that path will reside inside a compressed .jar file. Unity 5.4 and newer can use this API call with Streaming Assets just fine
3,WWW.LoadFromCacheOrDownload
using UnityEngine;
using System.Collections;
public class LoadFromCacheOrDownloadExample : MonoBehaviour
{
IEnumerator Start ()
{
while (!Caching.ready)
yield return null;
var www = WWW.LoadFromCacheOrDownload("http://myserver.com/myassetBundle", 5);
yield return www;
if(!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
yield return;
}
var myLoadedAssetBundle = www.assetBundle;
var asset = myLoadedAssetBundle.mainAsset;
}
}
4,UnityWebRequest
The UnityWebRequest has a specific API call to deal with AssetBundles. To begin, you’ll need to create your web request using UnityWebRequest.GetAssetBundle. After returning the request, pass the request object into DownloadHandlerAssetBundle.GetContent(UnityWebRequest). This GetContent call will return your AssetBundle object.
You can also use the assetBundle property on the DownloadHandlerAssetBundle class after downloading the bundle to load the AssetBundle with the efficiency of AssetBundle.LoadFromFile.
Here’s an example of how to load an AssetBundle that contains two GameObjects and Instantiate them. To begin this process, we’d just need to call StartCoroutine(InstantiateObject());
IEnumerator InstantiateObject()
{
string uri = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName; UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(uri, 0);
yield return request.Send();
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
GameObject cube = bundle.LoadAsset("Cube");
GameObject sprite = bundle.LoadAsset("Sprite");
Instantiate(cube);
Instantiate(sprite);
}
打包之后查看各個(gè)資源的大小日志
This information is available in the Editor Log just after you have performed the build. Go to the Console window (menu: Window < Console), click the small drop-down panel in the top right, and select Open Editor Log.
在unity中減少圖片的像素
try to reduce the physical size (in pixels) of the Texture images. To do this without modifying the actual source content, select the Texture in the Project view, and in the Inspector window reduce the Max Size. To see how this looks in-game, zoom in on a GameObject that uses the Texture, then adjust the Max Size until it starts looking worse in the Scene view. Changing the maximum Texture size does not affect your Texture Asset, just its resolution in the game.
By default, Unity compresses all Textures when importing. For faster workflow in the Editor, go to Unity < Preferences and untick the checkbox for Compress Assets on Import. All Textures are compressed in the build, regardless of this setting.
更多關(guān)于“unity培訓(xùn)”的問題,歡迎咨詢千鋒教育在線名師。千鋒教育多年辦學(xué),課程大綱緊跟企業(yè)需求,更科學(xué)更嚴(yán)謹(jǐn),每年培養(yǎng)泛IT人才近2萬人。不論你是零基礎(chǔ)還是想提升,都可以找到適合的班型,千鋒教育隨時(shí)歡迎你來試聽。