如何在unity 3D中制作动态模型?
How to make a dynamic model in unity 3D?
我有一个示例代码,其中它创建了鼠标点击的动态墙。
using UnityEngine;
using System.Collections;
public class CreateWalls : MonoBehaviour {
bool creating;
public GameObject start;
public GameObject end;
public GameObject wallPrehab;
GameObject wall;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
getinput();
}
/*
*this is the method for getting mouse click inputs
*/
void getinput(){
if (Input.GetMouseButtonDown (0)) {
setStart ();
} else if (Input.GetMouseButtonUp (0)) {
setEnd ();
} else {
if(creating){
adjust();
}
}
}
/*
* getting the mouse clicked position coordinate
*/
void setStart(){
creating = true;
start.transform.position = getWorldPoint ();
Debug.Log("Start = " + start.transform.position);
wall = (GameObject) Instantiate (wallPrehab, start.transform.position, Quaternion.identity);
}
/*
* getting the mouse click over position coordinate
*/
void setEnd(){
creating = false;
end.transform.position = getWorldPoint ();
Debug.Log("End = " + end.transform.position);
}
/*
* invoking the wall building method
*/
void adjust(){
end.transform.position = getWorldPoint ();
adjustWall ();
}
/*
* build the wall in between start point and the end point
*/
void adjustWall(){
start.transform.LookAt (end.transform.position);
end.transform.LookAt (start.transform.position);
float distance = Vector3.Distance (start.transform.position, end.transform.position);
wall.transform.position = start.transform.position + distance / 2 * start.transform.forward;
wall.transform.rotation = start.transform.rotation;
wall.transform.localScale = new Vector3 (wall.transform.localScale.x, wall.transform.localScale.y, distance);
}
/*
* method where the coordinate we taken from mouse click
*/
Vector3 getWorldPoint(){
Ray ray = GetComponent<Camera>().ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
return hit.point;
}
return Vector3.zero;
}
}
我有一些坐标,下图的每个角。
a=(55, 297)
b=(300, 297)
c=(55, 297)
d=(55, 52)
Above coordinates are for this image
现在我想为那些坐标建墙。像一个盒子。我不知道如何放置它们(坐标)。基本上一堵墙的坐标是 a(x,y) 和 b(x,y)。另一个是 b(x,y) 和 c(x,y)。谁能帮我解决这个问题我会很棒
您可以使用基元构建此墙,但这会为您提供默认的 10x10 顶点平面。这样效率不高,因为你花了很多不必要的飞机。
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.localScale = Vector3 (xsize, ysize, zsize);
cube.transform.position = Vector3(xpos, ypos, zpos);
最好的解决方案也是更复杂的是完全制作您自己的网格。
要做到这一点,你需要做一些事情,这里有一个例子来做一个简单的飞机:
GameObject plane = new GameObject("Plane");
MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
Mesh mymesh = new Mesh();
mymesh.name = "MyCustomMesh";
mymesh.vertices = new Vector3[] {
new Vector3(xpos, ypos, zpos),
new Vector3(xpos, ypos, zpos),
new Vector3(xpos, ypos, zpos),
new Vector3(xpos, ypos, zpos),
};
mymesh.uv = new Vector2[] {
new Vector2 (0, 0),
new Vector2 (0, 1),
new Vector2(1, 1),
new Vector2 (1, 0)
};
mymesh.triangles = new int[] { 0, 1, 2, 0, 2, 3};
mymesh.RecalculateNormals();
meshFilter.mesh = mymesh;
MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
我有一个示例代码,其中它创建了鼠标点击的动态墙。
using UnityEngine;
using System.Collections;
public class CreateWalls : MonoBehaviour {
bool creating;
public GameObject start;
public GameObject end;
public GameObject wallPrehab;
GameObject wall;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
getinput();
}
/*
*this is the method for getting mouse click inputs
*/
void getinput(){
if (Input.GetMouseButtonDown (0)) {
setStart ();
} else if (Input.GetMouseButtonUp (0)) {
setEnd ();
} else {
if(creating){
adjust();
}
}
}
/*
* getting the mouse clicked position coordinate
*/
void setStart(){
creating = true;
start.transform.position = getWorldPoint ();
Debug.Log("Start = " + start.transform.position);
wall = (GameObject) Instantiate (wallPrehab, start.transform.position, Quaternion.identity);
}
/*
* getting the mouse click over position coordinate
*/
void setEnd(){
creating = false;
end.transform.position = getWorldPoint ();
Debug.Log("End = " + end.transform.position);
}
/*
* invoking the wall building method
*/
void adjust(){
end.transform.position = getWorldPoint ();
adjustWall ();
}
/*
* build the wall in between start point and the end point
*/
void adjustWall(){
start.transform.LookAt (end.transform.position);
end.transform.LookAt (start.transform.position);
float distance = Vector3.Distance (start.transform.position, end.transform.position);
wall.transform.position = start.transform.position + distance / 2 * start.transform.forward;
wall.transform.rotation = start.transform.rotation;
wall.transform.localScale = new Vector3 (wall.transform.localScale.x, wall.transform.localScale.y, distance);
}
/*
* method where the coordinate we taken from mouse click
*/
Vector3 getWorldPoint(){
Ray ray = GetComponent<Camera>().ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
return hit.point;
}
return Vector3.zero;
}
}
我有一些坐标,下图的每个角。
a=(55, 297)
b=(300, 297)
c=(55, 297)
d=(55, 52)
Above coordinates are for this image
现在我想为那些坐标建墙。像一个盒子。我不知道如何放置它们(坐标)。基本上一堵墙的坐标是 a(x,y) 和 b(x,y)。另一个是 b(x,y) 和 c(x,y)。谁能帮我解决这个问题我会很棒
您可以使用基元构建此墙,但这会为您提供默认的 10x10 顶点平面。这样效率不高,因为你花了很多不必要的飞机。
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.localScale = Vector3 (xsize, ysize, zsize);
cube.transform.position = Vector3(xpos, ypos, zpos);
最好的解决方案也是更复杂的是完全制作您自己的网格。 要做到这一点,你需要做一些事情,这里有一个例子来做一个简单的飞机:
GameObject plane = new GameObject("Plane");
MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
Mesh mymesh = new Mesh();
mymesh.name = "MyCustomMesh";
mymesh.vertices = new Vector3[] {
new Vector3(xpos, ypos, zpos),
new Vector3(xpos, ypos, zpos),
new Vector3(xpos, ypos, zpos),
new Vector3(xpos, ypos, zpos),
};
mymesh.uv = new Vector2[] {
new Vector2 (0, 0),
new Vector2 (0, 1),
new Vector2(1, 1),
new Vector2 (1, 0)
};
mymesh.triangles = new int[] { 0, 1, 2, 0, 2, 3};
mymesh.RecalculateNormals();
meshFilter.mesh = mymesh;
MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;