using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
/// <summary>
/// 汽车管理类,记录信息并执行与汽车相关的功能效果。
/// </summary>
public class CarManage : MonoBehaviour
{
private List<Transform> allTrans;
private List<TransInfos> listInfos;
private TransInfos camInfos;
private TransInfos Infosold;
private TransInfos infos;
private List<Vector3> explodeEndPos;
private float radius=300;
private float changeRate = 0.1f;
public float distmove = 20f;
public int tmflag = 0;
public int ztmoveflag = 0;
private Shader transparentShader;
private Shader defShader;
GameObject cam,zt;
public Material carBodyMat;//车身材质
public GameObject[] carBody;
// Use this for initialization
void Start ()
{
allTrans = new List<Transform>();
listInfos = new List<TransInfos>();
explodeEndPos = new List<Vector3>();
RecordDefaultInfos(this.gameObject);
cam = GameObject.Find("Main Camera");
zt = GameObject.Find("外壳-1");
print(cam);
camInfos.defPos = cam.transform.position;
camInfos.defRot = cam.transform.rotation;
camInfos.defScal = cam.transform.localScale;
transparentShader = Shader.Find("Transparent/Diffuse");
defShader = Shader.Find("Standard");
SceneManage.carBodyColor = carBodyMat.GetColor("_DiffuseColor");
}
// Update is called once per frame
void Update ()
{
//ExplodeSphere();
Cursor.visible = true;
}
/// <summary>
/// 记录模型初始信息
/// </summary>
/// <param name="target"></param>
public void RecordDefaultInfos(GameObject target)
{
for (int i = 0; i < target.transform.childCount; i++)
{
infos.defPos = target.transform.GetChild(i).position;
infos.defRot = target.transform.GetChild(i).rotation;
infos.defScal = target.transform.GetChild(i).localScale;
allTrans.Add(target.transform.GetChild(i));
listInfos.Add(infos);
print(target.transform.GetChild(i).name+"-----"+infos.defPos);
}
}
private float j = 100;
/// <summary>
/// 圆形爆炸
/// </summary>
public void ExplodeSphere()
{
for (int i = 0; i < allTrans.Count; i++)
{
if(allTrans[i].name == "外壳-1")
{
continue;
}
float x = radius * Mathf.Cos(j);
float y = radius * Mathf.Sin(j);
allTrans[i].DOMove(new Vector3(x + listInfos[i].defPos.x, y + listInfos[i].defPos.y, listInfos[i].defPos.z), 1);
j += changeRate;
}
}
/// <summary>
/// 组合
/// </summary>
public void ReExplode()
{
for (int i = 0; i < allTrans.Count; i++)
{
allTrans[i].DOMove(listInfos[i].defPos, 1);
allTrans[i].DORotate(listInfos[i].defRot.eulerAngles, 1);
allTrans[i].DOScale(listInfos[i].defScal, 1);
}
}
public bool LocationCompare(Transform transform)
{
for (int i = 0; i < allTrans.Count; i++)
{
if (transform.name == allTrans[i].name)
{
float dist = Vector3.Distance(listInfos[i].defPos, transform.position);
print("Distance to other: " + dist);
if (dist< distmove)
{
allTrans[i].DOMove(listInfos[i].defPos, 1);
allTrans[i].DORotate(listInfos[i].defRot.eulerAngles, 1);
allTrans[i].DOScale(listInfos[i].defScal, 1);
}
}
}
return true;
}
public bool LocationHint(Transform transform)
{
distmove = 20000f;
return true;
}
public void CamFront()
{
cam.transform.DOMove(new Vector3(0, 0, 1400), 1);
cam.transform.rotation = Quaternion.Euler(0, 180, 0);
//cam.transform.DOMove(camInfos.defPos, 1);
//cam.transform.DORotate(camInfos.defRot.eulerAngles, 1);
cam.transform.DOScale(camInfos.defScal, 1);
}
public void CamBack()
{
cam.transform.DOMove(new Vector3(0, 0, -300), 1);
//public Quaternion rotation = Quaternion.Euler(new Vector3(90, 0, 0));
cam.transform.rotation = Quaternion.Euler(0, 0, 0);
//cam.transform.DORotate(camInfos.defRot.eulerAngles, 1);
//cam.transform.DOScale(camInfos.defScal, 1);
}
public void ztlocation0()
{
zt.transform.DOMove(new Vector3(329.3f, -244.799f, 760f), 1);
zt.transform.rotation = Quaternion.Euler(0, 90.00001f, -90.00001f);
//cam.transform.DOMove(camInfos.defPos, 1);
//cam.transform.DORotate(camInfos.defRot.eulerAngles, 1);
cam.transform.DOScale(camInfos.defScal, 1);
}
public void ztlocation1()
{
zt.transform.DOMove(new Vector3(1329.3f, -244.799f, 760f), 1);
zt.transform.rotation = Quaternion.Euler(0, 90.00001f, -90.00001f);
//cam.transform.DOMove(new Vector3(0, 0, -300), 1);
//public Quaternion rotation = Quaternion.Euler(new Vector3(90, 0, 0));
//cam.transform.rotation = Quaternion.Euler(0, 0, 0);
//cam.transform.DORotate(camInfos.defRot.eulerAngles, 1);
//cam.transform.DOScale(camInfos.defScal, 1);
}
/// <summary>
/// 改变车身颜色
/// </summary>
/// <param name="targetColor"></param>
public void ChangeColor(Color targetColor)
{
for (int i = 0; i < carBody.Length; i++)
{
foreach (Material item in GetMats(carBody[i]))
{
if (item.name == "CarbonDotPurpleMaterial (Instance)"&&item.shader==defShader)
{
item.DOColor(targetColor, "_DiffuseColor", 0.5f);
break ;
}
}
}
}
/// <summary>
/// 设置成透明状态
/// </summary>
public void SetModelTransparent(GameObject target)
{
if (tmflag == 1)
{
target.GetComponent<Renderer>().material.shader = transparentShader;
//target.GetComponent<Renderer>().material.color = new Color(Color.white.r, Color.white.g, Color.white.b, 0.4f);
//target.GetComponent<Renderer>().material.shader = defShader;
//target.GetComponent<Renderer>().material.color = new Color(Color.white.r, Color.white.g, Color.white.b, 0.4f);
}
else
{
target.GetComponent<Renderer>().material.shader = defShader;
target.GetComponent<Renderer>().material.SetColor("_DiffuseColor", SceneManage.carBodyColor);
}
//foreach (Material item in GetMats(target))
//{
// if (item.name == "CarbonDotPurpleMaterial (Instance)" && item.shader == defShader)
// {
// item.shader = transparentShader;
// item.color = new Color(Color.white.r, Color.white.g, Color.white.b, 0.4f);
// }
//}
}
public void SetModelTransparentold(GameObject target)
{
foreach (Material item in GetMats(target))
{
if (item.name == "CarbonDotPurpleMaterial (Instance)" && item.shader == defShader)
{
item.shader = transparentShader;
item.color = new Color(Color.white.r, Color.white.g, Color.white.b, 0.4f);
}
}
}
/// <summary>
/// 恢复成正常非透明状态
/// </summary>
public void SetOpaque(GameObject target)
{
foreach (Material item in GetMats(target))
{
if (item.name == "CarbonDotPurpleMaterial (Instance)")
{
item.shader = defShader;
item.SetColor("_DiffuseColor", SceneManage.carBodyColor);
}
}
}
/// <summary>
/// 恢复成正常非透明状态
/// </summary>
public void SetOpaqueold(GameObject target)
{
foreach (Material item in GetMats(target))
{
if (item.name == "CarbonDotPurpleMaterial (Instance)")
{
item.shader = defShader;
item.SetColor("_DiffuseColor", SceneManage.carBodyColor);
}
}
}
/// <summary>
/// 获取目标物体及子物体上所有材质
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
private List<Material> GetMats(GameObject target)
{
List<Material> changeMat = new List<Material>();
if (target.GetComponent<Renderer>() != null)
{
foreach (Material item in target.GetComponent<Renderer>().materials)
{
changeMat.Add(item);
}
}
foreach (Renderer item in target.GetComponentsInChildren<Renderer>())
{
foreach (Material mat in item.materials)
{
changeMat.Add(mat);
}
}
return changeMat;
}
private struct TransInfos
{
public Vector3 defPos;
public Quaternion defRot;
public Vector3 defScal;
}
}
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
/// <summary>
/// 汽车管理类,记录信息并执行与汽车相关的功能效果。
/// </summary>
public class CarManage : MonoBehaviour
{
private List<Transform> allTrans;
private List<TransInfos> listInfos;
private TransInfos camInfos;
private TransInfos Infosold;
private TransInfos infos;
private List<Vector3> explodeEndPos;
private float radius=300;
private float changeRate = 0.1f;
public float distmove = 20f;
public int tmflag = 0;
public int ztmoveflag = 0;
private Shader transparentShader;
private Shader defShader;
GameObject cam,zt;
public Material carBodyMat;//车身材质
public GameObject[] carBody;
// Use this for initialization
void Start ()
{
allTrans = new List<Transform>();
listInfos = new List<TransInfos>();
explodeEndPos = new List<Vector3>();
RecordDefaultInfos(this.gameObject);
cam = GameObject.Find("Main Camera");
zt = GameObject.Find("外壳-1");
print(cam);
camInfos.defPos = cam.transform.position;
camInfos.defRot = cam.transform.rotation;
camInfos.defScal = cam.transform.localScale;
transparentShader = Shader.Find("Transparent/Diffuse");
defShader = Shader.Find("Standard");
SceneManage.carBodyColor = carBodyMat.GetColor("_DiffuseColor");
}
// Update is called once per frame
void Update ()
{
//ExplodeSphere();
Cursor.visible = true;
}
/// <summary>
/// 记录模型初始信息
/// </summary>
/// <param name="target"></param>
public void RecordDefaultInfos(GameObject target)
{
for (int i = 0; i < target.transform.childCount; i++)
{
infos.defPos = target.transform.GetChild(i).position;
infos.defRot = target.transform.GetChild(i).rotation;
infos.defScal = target.transform.GetChild(i).localScale;
allTrans.Add(target.transform.GetChild(i));
listInfos.Add(infos);
print(target.transform.GetChild(i).name+"-----"+infos.defPos);
}
}
private float j = 100;
/// <summary>
/// 圆形爆炸
/// </summary>
public void ExplodeSphere()
{
for (int i = 0; i < allTrans.Count; i++)
{
if(allTrans[i].name == "外壳-1")
{
continue;
}
float x = radius * Mathf.Cos(j);
float y = radius * Mathf.Sin(j);
allTrans[i].DOMove(new Vector3(x + listInfos[i].defPos.x, y + listInfos[i].defPos.y, listInfos[i].defPos.z), 1);
j += changeRate;
}
}
/// <summary>
/// 组合
/// </summary>
public void ReExplode()
{
for (int i = 0; i < allTrans.Count; i++)
{
allTrans[i].DOMove(listInfos[i].defPos, 1);
allTrans[i].DORotate(listInfos[i].defRot.eulerAngles, 1);
allTrans[i].DOScale(listInfos[i].defScal, 1);
}
}
public bool LocationCompare(Transform transform)
{
for (int i = 0; i < allTrans.Count; i++)
{
if (transform.name == allTrans[i].name)
{
float dist = Vector3.Distance(listInfos[i].defPos, transform.position);
print("Distance to other: " + dist);
if (dist< distmove)
{
allTrans[i].DOMove(listInfos[i].defPos, 1);
allTrans[i].DORotate(listInfos[i].defRot.eulerAngles, 1);
allTrans[i].DOScale(listInfos[i].defScal, 1);
}
}
}
return true;
}
public bool LocationHint(Transform transform)
{
distmove = 20000f;
return true;
}
public void CamFront()
{
cam.transform.DOMove(new Vector3(0, 0, 1400), 1);
cam.transform.rotation = Quaternion.Euler(0, 180, 0);
//cam.transform.DOMove(camInfos.defPos, 1);
//cam.transform.DORotate(camInfos.defRot.eulerAngles, 1);
cam.transform.DOScale(camInfos.defScal, 1);
}
public void CamBack()
{
cam.transform.DOMove(new Vector3(0, 0, -300), 1);
//public Quaternion rotation = Quaternion.Euler(new Vector3(90, 0, 0));
cam.transform.rotation = Quaternion.Euler(0, 0, 0);
//cam.transform.DORotate(camInfos.defRot.eulerAngles, 1);
//cam.transform.DOScale(camInfos.defScal, 1);
}
public void ztlocation0()
{
zt.transform.DOMove(new Vector3(329.3f, -244.799f, 760f), 1);
zt.transform.rotation = Quaternion.Euler(0, 90.00001f, -90.00001f);
//cam.transform.DOMove(camInfos.defPos, 1);
//cam.transform.DORotate(camInfos.defRot.eulerAngles, 1);
cam.transform.DOScale(camInfos.defScal, 1);
}
public void ztlocation1()
{
zt.transform.DOMove(new Vector3(1329.3f, -244.799f, 760f), 1);
zt.transform.rotation = Quaternion.Euler(0, 90.00001f, -90.00001f);
//cam.transform.DOMove(new Vector3(0, 0, -300), 1);
//public Quaternion rotation = Quaternion.Euler(new Vector3(90, 0, 0));
//cam.transform.rotation = Quaternion.Euler(0, 0, 0);
//cam.transform.DORotate(camInfos.defRot.eulerAngles, 1);
//cam.transform.DOScale(camInfos.defScal, 1);
}
/// <summary>
/// 改变车身颜色
/// </summary>
/// <param name="targetColor"></param>
public void ChangeColor(Color targetColor)
{
for (int i = 0; i < carBody.Length; i++)
{
foreach (Material item in GetMats(carBody[i]))
{
if (item.name == "CarbonDotPurpleMaterial (Instance)"&&item.shader==defShader)
{
item.DOColor(targetColor, "_DiffuseColor", 0.5f);
break ;
}
}
}
}
/// <summary>
/// 设置成透明状态
/// </summary>
public void SetModelTransparent(GameObject target)
{
if (tmflag == 1)
{
target.GetComponent<Renderer>().material.shader = transparentShader;
//target.GetComponent<Renderer>().material.color = new Color(Color.white.r, Color.white.g, Color.white.b, 0.4f);
//target.GetComponent<Renderer>().material.shader = defShader;
//target.GetComponent<Renderer>().material.color = new Color(Color.white.r, Color.white.g, Color.white.b, 0.4f);
}
else
{
target.GetComponent<Renderer>().material.shader = defShader;
target.GetComponent<Renderer>().material.SetColor("_DiffuseColor", SceneManage.carBodyColor);
}
//foreach (Material item in GetMats(target))
//{
// if (item.name == "CarbonDotPurpleMaterial (Instance)" && item.shader == defShader)
// {
// item.shader = transparentShader;
// item.color = new Color(Color.white.r, Color.white.g, Color.white.b, 0.4f);
// }
//}
}
public void SetModelTransparentold(GameObject target)
{
foreach (Material item in GetMats(target))
{
if (item.name == "CarbonDotPurpleMaterial (Instance)" && item.shader == defShader)
{
item.shader = transparentShader;
item.color = new Color(Color.white.r, Color.white.g, Color.white.b, 0.4f);
}
}
}
/// <summary>
/// 恢复成正常非透明状态
/// </summary>
public void SetOpaque(GameObject target)
{
foreach (Material item in GetMats(target))
{
if (item.name == "CarbonDotPurpleMaterial (Instance)")
{
item.shader = defShader;
item.SetColor("_DiffuseColor", SceneManage.carBodyColor);
}
}
}
/// <summary>
/// 恢复成正常非透明状态
/// </summary>
public void SetOpaqueold(GameObject target)
{
foreach (Material item in GetMats(target))
{
if (item.name == "CarbonDotPurpleMaterial (Instance)")
{
item.shader = defShader;
item.SetColor("_DiffuseColor", SceneManage.carBodyColor);
}
}
}
/// <summary>
/// 获取目标物体及子物体上所有材质
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
private List<Material> GetMats(GameObject target)
{
List<Material> changeMat = new List<Material>();
if (target.GetComponent<Renderer>() != null)
{
foreach (Material item in target.GetComponent<Renderer>().materials)
{
changeMat.Add(item);
}
}
foreach (Renderer item in target.GetComponentsInChildren<Renderer>())
{
foreach (Material mat in item.materials)
{
changeMat.Add(mat);
}
}
return changeMat;
}
private struct TransInfos
{
public Vector3 defPos;
public Quaternion defRot;
public Vector3 defScal;
}
}