Unity3d学习笔记
 二维码 501
发表时间:2018-01-31 23:16

当对一个父GameObject进行无效设置后,它的子类gameobject也会无效,但是并没有改变子类的状态,也就是说你没有办法使用它自身的属性activeSelf,判断一个子gameobject是否是激活状态,要使用activeInHierarchy。如果要改变子类的状态,使用DeactivateChildren

使用transform的一些建议

1,最好把它的父transforn的位置设置为(0,0,0)这样对于它来说本地坐标和世界坐标是一样的

2,粒子系统的缩放不受transform的影响,需要去设置粒子发射器

3,Rigidbody的缩放也不受transform影响,需要在Rigidbody组件上面设置

4,修改父类的坐标会影响子类的本坐标


旋转的正确使用方法

错误一

   // 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;


   }

错误二

  // 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.<GameObject>("MyObject");

       Instantiate(prefab);

   }

这种方式是异步加载一组包含AssetBundle 数据的byte数组到内存中,可以添加CRC校验,如果使用了LZMA压缩,会自动解压

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.<GameObject>("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<GameObject>("Cube");

       GameObject sprite = bundle.LoadAsset<GameObject>("Sprite");

       Instantiate(cube);

       Instantiate(sprite);

   }


打包之后查看各个资源的大小日志

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.

https://docs.unity3d.com/uploads/Main/FileSizeOptimizationTexture.png