Unity 学习笔记汇总
官方API使用文档
导入MerchentGirl.unitypackage
包里的模型,位置如图所示。
若想要Game与Scene镜头效果保持一致,则可以先点击Main Camera,再点击GameObject下的Align With View。
新建脚本TestTransform
,如果需要更改脚本的默认编辑器,可以点击Edit->Preferences…->Extenternal Tools->External Script Editor
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestTransform : MonoBehaviour
{
public Transform cube, player;
// Start is called before the first frame update
void Start()
{
player = cube.transform.Find("Merchant_female@basice");
Debug.Log(player);
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestTransform : MonoBehaviour
{
public Transform cube, player;
// Start is called before the first frame update
void Start()
{
player = cube.transform.Find("Merchant_female@basice");//找不到对象
Debug.Log(player);
player = cube.transform.Find("Cube/Merchant_female@basice");//找不到对象
Debug.Log(player);
player = cube.transform.Find("Sphere/Merchant_female@basice");//能找到对象
Debug.Log(player);
}
// Update is called once per frame
void Update()
{
}
}
using UnityEngine;
public class Test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log(transform.localPosition);
Debug.Log(transform.localScale);
Debug.Log(transform.localEulerAngles);
}
// Update is called once per frame
void Update()
{
}
}
using UnityEngine;
public class Test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log(transform.localPosition);
Debug.Log(transform.localScale);
Debug.Log(transform.localEulerAngles);
}
// Update is called once per frame
void Update()
{
}
}
using UnityEngine;
public class Test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log(transform.localPosition);
Debug.Log(transform.localScale);
Debug.Log(transform.localEulerAngles);
Debug.Log(transform.right);//1,0,0 x轴
Debug.Log(transform.up);//0,1,0 y轴
Debug.Log(transform.forward);//0,0,1 z轴
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.A))
{
transform.localPosition = new Vector3(7.7f, -4.26f, 0.9f);
}
}
}
Public Method | Description |
---|---|
GetChild | Returns a transform child by index. |
IsChildOf | Is this transform a child of parent? |
新建Cube、Sphere对象,Test脚本,将Test挂载到Main Camera上,然后把Cube、Sphere与Test中的变量进行绑定。
using UnityEngine;
public class test : MonoBehaviour
{
public Transform cube, sphere;
// Start is called before the first frame update
void Start()
{
//GetChild Returns a transform child by index.
Debug.Log(cube.transform.GetChild(0));
//IsChildOf Is this transform a child of parent?
Debug.Log(sphere.transform.IsChildOf(cube.transform));
}
// Update is called once per frame
void Update()
{
}
}
单击对象,再单击Element 0对应的小圆圈
通过上下滑动鼠标滚轮浏览所有的Material,然后点击选中所需要的Material
using UnityEngine;
public class TestTranslate : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//每秒前进一次
transform.Translate(Vector3.forward * Time.deltaTime);
}
}
会看到对象朝Z轴移动
using UnityEngine;
public class TestTranslate : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//transform.Translate(Vector3.forward * Time.deltaTime, Space.World); //世界坐标下的坐标
//transform.Translate(Vector3.forward * Time.deltaTime, Space.Self); //本地坐标下的坐标
transform.Translate(transform.forward * Time.deltaTime, Space.World); //世界坐标下的坐标
//transform.Translate(transform.forward * Time.deltaTime, Space.Self); //本地坐标下的坐标
}
}
世界坐标下的与本地坐标下的移动Vector3.forward或transform.forward,会有所不同。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestRotate : MonoBehaviour
{
public float speed = 100f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Rotate(Vector3.right * speed * Time.deltaTime, Space.World);
}
}
会看到两个对象都在旋转
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotateAround : MonoBehaviour
{
// Start is called before the first frame update
GameObject cube, player;
void Start()
{
cube = GameObject.Find("Cube");
player = GameObject.Find("Merchant_female@basice");
}
// Update is called once per frame
void Update()
{
player.transform.RotateAround(cube.transform.position, Vector3.up, 10);
}
}
会看到人物围绕着四方体旋转。
本次使用中文编程,即变量名、类名的命名使用中文命名。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class 父子关系 : MonoBehaviour
{
// Start is called before the first frame update
GameObject 立方体, 胶囊, 圆柱;
void Start()
{
立方体 = GameObject.Find("Cube");
圆柱 = GameObject.Find("Cylinder");
Debug.Log(圆柱.transform.parent);
Debug.Log(圆柱.transform.parent.parent);
Debug.Log(圆柱.transform.root);
Debug.Log(立方体.transform.childCount);
//解除与立方体的父子关系
//立方体.transform.DetachChildren();
//Debug.Log(立方体.transform.childCount);
DetachAllChildren(立方体.transform);
}
// Update is called once per frame
void Update()
{
}
void DetachAllChildren(Transform transform)
{
if (transform.childCount == 0)
{
return;
}
for (int i = 0; i < transform.childCount; i++)
{
DetachAllChildren(transform.GetChild(i));
}
transform.DetachChildren();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Clock : MonoBehaviour
{
GameObject bg, s, m, h;
// Start is called before the first frame update
void Start()
{
bg = GameObject.Find("表框");
s = GameObject.Find("秒针");
m = GameObject.Find("分针");
h = GameObject.Find("时针");
s.transform.RotateAround(bg.transform.position, Vector3.forward, DateTime.Now.Second * 6f);
m.transform.RotateAround(bg.transform.position, Vector3.forward, (DateTime.Now.Minute + DateTime.Now.Second / 60f) * 6f);
h.transform.RotateAround(bg.transform.position, Vector3.forward, (DateTime.Now.Hour + DateTime.Now.Minute / 60f) * 30f);
}
// Update is called once per frame
void Update()
{
s.transform.RotateAround(bg.transform.position, Vector3.forward, Time.deltaTime * 6);
m.transform.RotateAround(bg.transform.position, Vector3.forward, Time.deltaTime * 0.1f);
h.transform.RotateAround(bg.transform.position, Vector3.forward, Time.deltaTime * 0.05f/6);
}
}