Unity中搭建场景(unity2019.4.20)
第一步,将编译器修改为vs
Unity 中Edit–>Preferences–>External Tools ,修改 External Script Editor 为 VSCode,如图所示
unity3d中物体移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class object1 : MonoBehaviour
{
public Rigidbody rd;
//public Text scoreText;
// Start is called before the first frame update
void Start()
{
rd = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");//渐变的1、-1 a,d或键盘左右方向
float v = Input.GetAxis("Vertical");//w,s或键盘上下方向
rd.AddForce(new Vector3(h, 0, v) * 3);
}
//private void OnCollisionEnter(Collision collision)//碰撞的系统事件
//{
// if (collision.gameObject.tag == "Food")
// {
// Destroy(collision.gameObject);
// }
//}
}
箭头指向方向
1.制作Shader
(1)首先,将我们的图片导入:
右键另存为,保存到项目中。
(2)新建Shader,在Project视图中选择Create→Shader→Standard Surface Shader
创建好的如下,无需修改名字
双击进入vs,将代码修改为如下代码
Shader "Custom/NavPathArrow"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_ScrollYSpeed("Y Scroll Speed", Range(-20, 20)) = 2
}
SubShader
{
Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
LOD 100
//双面渲染
Cull Off
//Alpha混合
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _ScrollYSpeed;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed2 uv = i.uv;
uv.y += _ScrollYSpeed * _Time;
fixed4 col = tex2D(_MainTex, uv);
return col;
}
ENDCG
}
}
}
2.制作材质球
新建材质球,在Project视图中选择Create→Material,命名为NavPathArrow:
设置材质球的属性,将Shader设置为Custom/NavPathArrow(在Custom中选择,如下图),将贴图拖进去:
3.制作预制体
(1)新建一个Quad对象,在Hierarchy视图,右击选择Create→Quad:
将它的材质球换成刚才我们制作的材质球:
拖到Project视图,做成预制体:
4.实现代码
新建脚本NavPathArrow.cs,双击打开脚本,编写代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 导航箭头
/// </summary>
public class NavPathArrow : MonoBehaviour
{
public MeshRenderer meshRenderer;//箭头3D对象Quad
public List<Transform> points = new List<Transform>();//路径点
private List<MeshRenderer> lines = new List<MeshRenderer>();//显示的路径
public float xscale = 1f;//缩放比例
public float yscale = 1f;
void Start()
{
//箭头宽度缩放值
xscale = meshRenderer.transform.localScale.x;
//箭头长度缩放值
yscale = meshRenderer.transform.localScale.y;
}
//画路径
public void DrawPath()
{
if (points == null || points.Count <= 1)
return;
for (int i = 0; i < points.Count - 1; i++)
{
DrawLine(points[i].position, points[i + 1].position, i);
}
}
//画路径 参数为路径点数组
public void DrawPath(Vector3[] points)
{
if (points == null || points.Length <= 1)
return;
for (int i = 0; i < points.Length - 1; i++)
{
DrawLine(points[i], points[i + 1], i);
}
}
//隐藏路径
public void HidePath()
{
for (int i = 0; i < lines.Count; i++)
lines[i].gameObject.SetActive(false);
}
//画路径
private void DrawLine(Vector3 start, Vector3 end, int index)
{
Debug.Log(transform.gameObject.name);
MeshRenderer mr;
if (index >= lines.Count)
{
mr = Instantiate(meshRenderer);
lines.Add(mr);
}
else
{
mr = lines[index];
}
var tran = mr.transform;
var length = Vector3.Distance(start, end);
tran.localScale = new Vector3(xscale, length, 1);
tran.position = (start + end) / 2;
//指向end
tran.LookAt(end);
//旋转偏移
tran.Rotate(90, 0, 0);
mr.material.mainTextureScale = new Vector2(1, length * yscale);
mr.gameObject.SetActive(true);
}
void OnGUI()
{
if (GUI.Button(new Rect(20, 40, 80, 20), "显示路径"))
{
DrawPath();
}
if (GUI.Button(new Rect(20, 80, 80, 20), "隐藏路径"))
{
HidePath();
}
}
}
实现功能
新建两个Cube作为路径点,一个Plane作为地板:
把脚本附给Plane,然后把路径点也就是两个Cube,然后预制体Quad拖进去:
注意:要把Quad中的Mesh Collider取消!否则会有碰撞体积!