千锋教育-做有情怀、有良心、有品质的职业教育机构

手机站
千锋教育

千锋学习站 | 随时随地免费学

千锋教育

扫一扫进入千锋手机站

领取全套视频
千锋教育

关注千锋学习站小程序
随时随地免费学习课程

当前位置:首页  >  技术干货  > unity学习笔记(一)

unity学习笔记(一)

来源:千锋教育
发布人:qyf
时间: 2022-07-19 17:18:00 1658222280

  当对一个父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.("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.("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);

  }

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

  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.

1

  更多关于“unity培训”的问题,欢迎咨询千锋教育在线名师。千锋教育多年办学,课程大纲紧跟企业需求,更科学更严谨,每年培养泛IT人才近2万人。不论你是零基础还是想提升,都可以找到适合的班型,千锋教育随时欢迎你来试听。

tags:
声明:本站稿件版权均属千锋教育所有,未经许可不得擅自转载。
10年以上业内强师集结,手把手带你蜕变精英
请您保持通讯畅通,专属学习老师24小时内将与您1V1沟通
免费领取
今日已有369人领取成功
刘同学 138****2860 刚刚成功领取
王同学 131****2015 刚刚成功领取
张同学 133****4652 刚刚成功领取
李同学 135****8607 刚刚成功领取
杨同学 132****5667 刚刚成功领取
岳同学 134****6652 刚刚成功领取
梁同学 157****2950 刚刚成功领取
刘同学 189****1015 刚刚成功领取
张同学 155****4678 刚刚成功领取
邹同学 139****2907 刚刚成功领取
董同学 138****2867 刚刚成功领取
周同学 136****3602 刚刚成功领取
相关推荐HOT