Unity 3D随笔

改变位移:

  1. transform.position
  2. 刚体组件位移Rigidbody2d.MovePosition(position);
    此方式不受重力影响
  3. 给游戏系统添加一个力

身体碰撞luzhang (UnityEngine.BoxCollider)
UnityEngine.MonoBehaviour:print (object)
PeopleAction:OnTriggerEnter (UnityEngine.Collider) (at Assets/Script/PeopleAction.cs:93)

随机生成地图

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// PR_sc
/// </summary>
public class MapManager : MonoBehaviour {
	//利用脚本生成地图。 10*10的像素地图

	public GameObject[] OutWallArray;
	public GameObject[] FloorArray;
	public GameObject[] WallArray;

	public int rows = 10;  //定义地图的行列。
	public int cols = 10;

	public int minCountWall = 2;
	public int maxCountWall = 8;

	private Transform mapHolder;
	private List<Vector2> positionList = new List<Vector2>();

	// Use this for initialization
	void Start () {
		InitMap();
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	private void InitMap()
	{
		mapHolder = new GameObject("Map").transform;// 设置一个父类管理生成的地图
		for(int x = 0; x < cols; x++)
		{
			for(int y= 0; y < rows; y++)
			{
				if(x==0 ||y==0 || x == cols - 1 || y == rows - 1)//地图最外面一圈是围墙
				{
					int index = Random.Range(0, OutWallArray.Length);
					GameObject go = GameObject.Instantiate(OutWallArray[index], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
					go.transform.SetParent(mapHolder);
				}
				else// 其余是地板
				{
					int index = Random.Range(0, FloorArray.Length);
					GameObject go = GameObject.Instantiate(FloorArray[index], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
					go.transform.SetParent(mapHolder);
				}
			}
		}
		positionList.Clear();
		for (int x = 2; x < cols - 2; x++)
		{
			for(int y=2; y < rows - 2; y++)
			{
				positionList.Add(new Vector2(x, y));
			}
		}
		//创建障碍物 食物 敌人
		//创建障碍物
		int WallCount = Random.Range(minCountWall, maxCountWall + 1);//随机生成障碍物个数的范围
		for(int i = 0; i < WallCount; i++)
		{
			//随机取得位置
			int positionIndex = Random.Range(0, positionList.Count);
			Vector2 pos = positionList[positionIndex];
			positionList.RemoveAt(positionIndex);
			//随机取得障碍物
			int WallIndex = Random.Range(0, WallArray.Length);
			GameObject go = GameObject.Instantiate(WallArray[WallIndex], pos, Quaternion.identity) as GameObject;
			go.transform.SetParent(mapHolder);
		}
	}
}

加载资源

  • 如果需要动态加载资源,则需要把资源放在Resources文件夹中,这样打包资源的时候会打包到程序中

Unity3D 里有两种动态加载机制:一个是Resources.Load,另外一个通过AssetBundle,其实两者区别不大。 Resources.Load就是从一个缺bai省打进程序包里的AssetBundle里加载资源,而一般AssetBundle文件需要你自己创建,运行时动态加载,可以指定路径和来源的。
(1).assetBundle就是内部数据读取完后自动创建了一个assetBundle而已Create完以后,等于把硬盘或者网络的一个文件读到内存一个中,这时也就是个AssetBundle内存镜像数据块。释放方式是AssetBundle.Unload(false)

 		GameObject preb = Resources.Load<GameObject>(path);//加载资源
        GameObject go = GameObject.Instantiate<GameObject>(preb);//实例化
        go.transform.SetParent(Selection.activeTransform, false);//加载节点 false不受父节点影响

(2).用AssetBundle.Load(同Resources.Load) 会从AssetBundle的内存镜像里读取并创建一个Asset对象,使用Resources.UnloadUnusedAssets()释放全部和Resources.UnloadAsset(gameobject);释放单个;

  • Instaniate一个Prefab,是一个对Assets进行Clone(复制)+引用结合的过程,使用GameObject.Destroy(gameobject);
    (注意)游戏对象可能不是动态加载时,但是可能他的材质、图集是动态加载的请把这些应用置为空。

重载效果 将组建重新加载一次.

  • 加载预制体信息
    Vector3 length = go.GetComponent().mesh.bounds.size;
    float xlength = length.x * transform.lossyScale.x;
    float ylength = length.y * transform.lossyScale.y;
    float zlength = length.z * transform.lossyScale.z;

  • unity3d:监听当前动画执行完毕回调

    public static IEnumerator YieldAniFinish(Animator ani,string aniName, UnityAction action)
    {
    yield return null;
    AnimatorStateInfo stateinfo = ani.GetCurrentAnimatorStateInfo(0);

      if (stateinfo.IsName(aniName) && (stateinfo.normalizedTime > 1.0f))
      {
          action();
      }
      else
      {
          Instance.StartCoroutine(YieldAniFinish(ani,aniName, action));
      }
    

    }

使用

StartCoroutine(PublicFunc.YieldAniFinish(m_ani, “Shang”, () => { TaskFinish(); }));

错误

Step Offset must be less or equal to + * 2

UNITY-名称“ AssetDatabase”在当前上下文中不存在

		似乎在您使用的脚本之一中,AssetDatabase该脚本属于UnityEditor名称空间。它仅存在于Unity编辑器内部。
		在构建中,命名空间UnityEditor不存在->您不能在构建的应用程序中使用任何名称空间!
		因此,在意识到基本有解决方案之后,如何使用例如自定义编辑器脚本或某些代码块(仅在Unity编辑器内部而在构建中不存在)修复这些错误:
		请确保所有仅是编辑器脚本的脚本都放在名为的文件夹中Editor。Unity自动排除构建中的那些。
		或将#if预处理器与一起使用UNITY_EDITOR:
		#if UNITY_EDITOR
		    using UnityEditor;
		#endif
		...
		#if UNITY_EDITOR
		    //some code here that uses something from the UnityEditor namespace
		#endif

Log

2021-03-08 10:05:35.543 6647-6683/com.DefaultCompany.NewUnityProjectT I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib/hw/gralloc.m7221.so from the current namespace instead.
2021-03-08 10:05:35.549 6647-6683/com.DefaultCompany.NewUnityProjectT I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib/hw/gralloc.m7221.so from the current namespace instead.
2021-03-08 10:05:35.584 6647-6683/com.DefaultCompany.NewUnityProjectT I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib/hw/gralloc.m7221.so from the current namespace instead.
2021-03-08 10:05:35.810 6647-6728/com.DefaultCompany.NewUnityProjectT W/Unity: The referenced script (PlaneMapT) on this Behaviour is missing!
2021-03-08 10:05:35.813 6647-6728/com.DefaultCompany.NewUnityProjectT W/Unity: The referenced script (PlaneMapT) on this Behaviour is missing!
2021-03-08 10:05:35.814 6647-6728/com.DefaultCompany.NewUnityProjectT W/Unity: The referenced script (PlaneMapT) on this Behaviour is missing!
2021-03-08 10:05:35.821 6647-6728/com.DefaultCompany.NewUnityProjectT W/Unity: The referenced script (AddRoadBlock) on this Behaviour is missing!
2021-03-08 10:05:35.839 6647-6728/com.DefaultCompany.NewUnityProjectT W/Unity: The referenced script on this Behaviour (Game Object 'Plane') is missing!
2021-03-08 10:05:35.839 6647-6728/com.DefaultCompany.NewUnityProjectT W/Unity: The referenced script on this Behaviour (Game Object 'moto_modeling 0.1 (1)') is missing!
2021-03-08 10:05:35.840 6647-6728/com.DefaultCompany.NewUnityProjectT W/Unity: The referenced script on this Behaviour (Game Object 'action') is missing!
2021-03-08 10:05:35.840 6647-6728/com.DefaultCompany.NewUnityProjectT W/Unity: The referenced script on this Behaviour (Game Object 'moto_modeling 0.1') is missing!
2021-03-08 10:05:36.038 6647-6647/com.DefaultCompany.NewUnityProjectT I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib/hw/gralloc.m7221.so from the current namespace instead.
2021-03-08 10:05:37.840 6647-6683/com.DefaultCompany.NewUnityProjectT I/Unity: 身体碰撞action (UnityEngine.CharacterController)
    UnityEngine.Logger:Log(LogType, Object)
2021-03-08 10:05:37.841 6647-6683/com.DefaultCompany.NewUnityProjectT I/Unity: 身体碰撞action (UnityEngine.BoxCollider)
    UnityEngine.Logger:Log(LogType, Object)

补充说明注意:
Center全是0,0,0
size为1,1,1
Center表示碰撞器以0,0,0位原点画了一个size位1,1,1的立方体作为碰撞器区域,其实这个时候碰撞器区域一半在地面上面,一半在地面下面。
这样肯定是有问题的,所以运行后,模型就下坠了。

要解决此问题,就不能让碰撞器和地面交叉,怎么办呢?
把Center改为0,0.5,0,然后会发现碰撞区域都在地面上了,然后也就不会下坠了。

有的人也说打开刚体的is Kinematic开关就行了,打开确实是行了,但是这个时候碰撞效果,只能是自己操作坐标实现了。
官方对它的解释:

  • Is Kinematic 是否是运动学
    If enabled, the object will not be driven by the physics engine, and can only be manipulated by its Transform. This is useful for moving platforms or if you want to animate a Rigidbody that has a HingeJoint attached.
    若激活,该物体不再受物理引擎驱动,而只能通过变换来操作。适用于模拟运动的平台或者模拟受铰链关节连接的刚体。

  • 另外碰撞器上的IsTrigger属性需要注意:

  1. 当IsTrigger=false时,碰撞器根据物理引擎引发碰撞,产生碰撞的效果,可以调用OnCollisionEnter/Stay/Exit函数;
  2. 当IsTrigger=true时,碰撞器被物理引擎所忽略,没有碰撞效果,可以调用OnTriggerEnter/Stay/Exit函数。

碰撞

//发生碰撞时 触发一次
OnCollisionEnter();
//碰撞持续时 一直触发
OnCollisionStay();
//碰撞离开时 触发一次
ONCollisionExit();

isTrigger 触发器–>可以穿过碰撞器物体

	OnTriggerEnter 用于检测其他碰撞器的触发器

物体的MeshFilter MeshRender
作用:对物体进行渲染 去掉物体样式消失,占位不变

unity网站;

4种光

  1. directional light 平行光
  2. point light 点光
  3. spot light 光束(手电筒)
  4. area light 区域光
  • 光照贴图烘焙

window–Rendering —light---->Generate Lighting

选中所有与灯光相关的场景
模式使用 static
场景:light–>mode |mix 混合 烘焙运行时都可以
|baked 只用于烘焙
|realtime 运行时效果
ShawdDown:Soft Shadows
light选项:
mixed Lighting -->Lighting Mode
然后在Generate Lighting执行

unity中三种调用其他脚本函数的方法
第一种,被调用脚本函数为static类型,调用时直接用 脚本名.函数名()。很不实用……
第二种,GameObject.Find(“脚本所在物体名”).SendMessage(“函数名”); 此种方法可以调用public和private类型函数
第三种,GameObject.Find(“脚本所在物体名”).GetComponent<脚本名>().函数名();此种方法只可以调用public类型函数

打包后在android运行不能动态加载资源

Resources文件夹

* Resources文件夹是Unity中标志性的目录,这个目录下的资源无论是否有引用关系,都会被强制打在游戏包中
* Resources文件夹可以是顶层目录,也可以是某个文件夹的子目录,**打包后Unity会自动将它们合并在一起** 在代码中动态读取

for example :
目录- |New Folder |
Prefab|
|Cube
|New Material //材质
|Resources |B //贴图
|Scene
|Script_

		//读取材质  注意文件路径
		Material material = Resources.Load<Material>("New Material");
		//读取贴图
		Texture texture = Resources.Load<Textrue>("B");
		//读取prefab 
		GameObject profab = Resources.Load<GameObject>("Cube");
		//实例化游戏对象
		GameObject go = GameObject.Instantiate<GameObject>(profab);
		//挂载主摄像机节点下

AssetBundle(待补全)

选中对象-->Inspector属性 最下边对象名称:AssetBundle _1__  __2_  1指定名称+ 2后缀 
可以指定目录 1:sence/wall 

AndroidJavaClass jc = new AndroidJavaClass(“unity生成应用包名”);
AndroidJavaObject jo = jc.GetStatic(“currentActivity”);
jo.Call(“unity自己的方法”);

UnityPlayer.UnitySendMessage(“Canvas/Show”,“ShowText”,“Android Message”);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值