327 lines
10 KiB
C#
327 lines
10 KiB
C#
using System.Collections;
|
||
using UnityEngine.SceneManagement;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using System.IO;
|
||
using TMPro;
|
||
using System.Threading;
|
||
using Cinemachine;
|
||
using DG.Tweening;
|
||
|
||
public class building : MonoBehaviour
|
||
{
|
||
public GameObject buildingModels;
|
||
private GameObject temp;
|
||
public Image Transitions;
|
||
public float speed = .5f;
|
||
public string buildingName;
|
||
public static int backTo = 3;
|
||
public TMP_Text descriptionText;
|
||
public Image nameTextures;
|
||
|
||
public GameObject findBuildingG;
|
||
public GameObject Camera;
|
||
public GameObject vcam;
|
||
public GameObject vcamA;
|
||
|
||
public TMP_Text zi;
|
||
public Image noMN;
|
||
public Image gu;
|
||
public Transform gu2;
|
||
public Image jin;
|
||
public Image jin2;
|
||
public Image wz;
|
||
public VerticalLayoutGroup Content;
|
||
public GameObject XX;
|
||
|
||
int bd = 0;
|
||
GameObject disBuilding;
|
||
Transform buildingTarget;
|
||
string textContent;
|
||
|
||
void Start()
|
||
{
|
||
//这里是因为我要回到两个场景,一个是主页,一个是建筑页面,所以做了判断,可以不用管
|
||
if (backTo == 0)
|
||
{
|
||
StartCoroutine(SceneLoadIn());
|
||
}
|
||
|
||
if (backTo == 1)
|
||
{
|
||
//StartCoroutine(SceneLoadIn());
|
||
buildingName = GameObject.Find("buildingTarget").GetComponent<building>().buildingName;
|
||
Debug.Log(buildingName);
|
||
temp = (GameObject)Instantiate(Resources.Load(buildingName), buildingModels.transform);
|
||
temp.transform.parent = buildingModels.transform;
|
||
|
||
nameTextures = GameObject.Find("nameImage").GetComponent<Image>();
|
||
createImage();
|
||
LoadDescriptionsFromFile();
|
||
}
|
||
|
||
if (backTo == 2)
|
||
{
|
||
StartCoroutine(SceneLoadIn());
|
||
GameObject btn = GameObject.Find("backbutton");
|
||
btn.GetComponent<Button>().onClick.Invoke();
|
||
}
|
||
}
|
||
|
||
public void getName(string name)//放在要跳转的建筑的按钮上,在name里输入这个建筑的代号,方便在下个场景里实例化出来对应建筑
|
||
{
|
||
buildingName = name;
|
||
}
|
||
|
||
private void LoadDescriptionsFromFile()//根据getName里的名称,加载建筑模型,以及模型的介绍文字
|
||
{
|
||
if (!File.Exists(Application.streamingAssetsPath + "/building.txt"))
|
||
{
|
||
Debug.LogError("文件不存在!");
|
||
return;
|
||
}
|
||
// 读取文件内容
|
||
string[] lines = File.ReadAllLines(Application.streamingAssetsPath + "/building.txt");
|
||
|
||
// 解析文件内容
|
||
for (int i = 0; i < lines.Length; i++)
|
||
{
|
||
string[] data = lines[i].Split('@');
|
||
if (data.Length >= 2)
|
||
{
|
||
string itemName = data[0];
|
||
string description = data[1];
|
||
}
|
||
if (data[0] == buildingName)
|
||
{
|
||
descriptionText.text = data[1];
|
||
descriptionText.text = descriptionText.text.Replace("\\n", "\n");
|
||
return;
|
||
}
|
||
}
|
||
|
||
}
|
||
public void ToScene(string SceneName)//跳转到指定场景,SceneName可以在button处自己填上
|
||
{
|
||
backTo = 1;
|
||
StartCoroutine(SceneLoadOut(SceneName));
|
||
}
|
||
|
||
IEnumerator SceneLoadOut(string SceneName)
|
||
{
|
||
//goto
|
||
Color tempColor = Transitions.color;
|
||
tempColor.a = 0;
|
||
Transitions.color = tempColor;
|
||
while (Transitions.color.a < 1)
|
||
{
|
||
Transitions.color += new Color(0, 0, 0, speed * Time.deltaTime);
|
||
yield return null;
|
||
}
|
||
|
||
SceneManager.LoadScene(SceneName);
|
||
DontDestroyOnLoad(this.gameObject);
|
||
}//出场效果
|
||
|
||
IEnumerator SceneLoadIn()//入场效果
|
||
{
|
||
Color tempColor = Transitions.color;
|
||
tempColor.a = 1;
|
||
Transitions.color = tempColor;
|
||
while (Transitions.color.a > 0)
|
||
{
|
||
Transitions.color += new Color(0, 0, 0, -speed * Time.deltaTime);
|
||
yield return null;
|
||
}
|
||
/* GameObject btn = GameObject.Find("Button_JZ");
|
||
btn.GetComponent<Button>().onClick.Invoke();*/
|
||
}
|
||
public void backScene(string SceneName)
|
||
{
|
||
//back
|
||
backTo = 2;
|
||
SceneManager.LoadScene(SceneName);
|
||
GameObject aa = GameObject.Find("buildingTarget");
|
||
Destroy(aa, 1f);
|
||
}//返回建筑介绍菜单
|
||
public void backMain(string SceneName)
|
||
{
|
||
//back
|
||
backTo = 0;
|
||
SceneManager.LoadScene(SceneName);
|
||
GameObject aa = GameObject.Find("buildingTarget");
|
||
Destroy(aa, .5f);
|
||
}//返回主菜单
|
||
|
||
void createImage()
|
||
{
|
||
Sprite spriteName = Resources.Load<Sprite>("image/" + buildingName + "_image");
|
||
nameTextures.sprite = spriteName;
|
||
}//创建建筑对应名称的image
|
||
|
||
//public void findBuilding()
|
||
//{
|
||
// if (bd == 1)
|
||
// {
|
||
// disBuilding.SetActive(false);
|
||
// }
|
||
// if (bd == 1 && buildingTarget.name == buildingName)
|
||
// {
|
||
// ToScene("building");
|
||
// }
|
||
// Camera.GetComponent<CinemachineBrain>().enabled = false;
|
||
// Camera.GetComponent<mouseMove>().enabled = true;
|
||
|
||
// buildingTarget = findBuildingG.transform.Find(buildingName);
|
||
// Camera.GetComponent<mouseMove>().target = buildingTarget;
|
||
// disBuilding = buildingTarget.transform.Find("buildingCanvas").gameObject;
|
||
// disBuilding.SetActive(true);
|
||
// Camera.GetComponent<mouseMove>().y = 38f;
|
||
|
||
// bd = 1;
|
||
|
||
//}//这个是在建筑场景里聚焦选中模型用的,可以删掉
|
||
public void findBuilding()
|
||
{
|
||
if (bd == 1)
|
||
{
|
||
disBuilding.SetActive(false);
|
||
Debug.Log("--------");
|
||
}
|
||
if (bd == 1 && buildingTarget.name == buildingName)
|
||
{
|
||
ToScene("building");
|
||
}
|
||
Camera.GetComponent<CinemachineBrain>().enabled = true;
|
||
//Camera.GetComponent<mouseMove>().enabled = false;
|
||
buildingTarget = findBuildingG.transform.Find(buildingName);
|
||
vcamA.SetActive(false);
|
||
vcam.SetActive(true);
|
||
|
||
vcam.GetComponent<CinemachineVirtualCamera>().Follow = buildingTarget;
|
||
disBuilding = buildingTarget.transform.Find("buildingCanvas").gameObject;
|
||
disBuilding.SetActive(true);
|
||
bd = 1;
|
||
}
|
||
|
||
public void findBuildingInvoke(float a)
|
||
{
|
||
Invoke("findBuilding", a);
|
||
}
|
||
|
||
public void findB2()
|
||
{
|
||
//if (bd == 1)
|
||
//{
|
||
// disBuilding.SetActive(false);
|
||
// Debug.Log("--------");
|
||
//}
|
||
Camera.GetComponent<CinemachineBrain>().enabled = true;
|
||
Camera.GetComponent<mouseMove>().enabled = false;
|
||
buildingTarget = findBuildingG.transform.Find(buildingName);
|
||
vcamA.SetActive(false);
|
||
vcam.SetActive(true);
|
||
|
||
vcam.GetComponent<CinemachineVirtualCamera>().Follow = buildingTarget;
|
||
disBuilding = buildingTarget.transform.Find("noMBCanvas").gameObject;
|
||
disBuilding.SetActive(true);
|
||
|
||
if (!File.Exists(Application.streamingAssetsPath + "/XinXi.txt"))
|
||
{
|
||
Debug.LogError("文件不存在!");
|
||
return;
|
||
}
|
||
string[] lines = File.ReadAllLines(Application.streamingAssetsPath + "/XinXi.txt");
|
||
zi.text = null;
|
||
|
||
for (int i = 0; i < lines.Length; i++)
|
||
{
|
||
string[] data = lines[i].Split('@');
|
||
if (data.Length >= 2)
|
||
{
|
||
string itemName = data[0];
|
||
string description = data[1];
|
||
}
|
||
if (data[0] == buildingName)
|
||
{
|
||
zi.text = data[1];
|
||
zi.text = zi.text.Replace("\\n", "\n");
|
||
}
|
||
}
|
||
|
||
Sprite spriteName = Resources.Load<Sprite>("noModels/gu/" + buildingName);
|
||
gu.sprite = spriteName;
|
||
imageHD(spriteName, gu);
|
||
Sprite jin1 = Resources.Load<Sprite>("noModels/jin/" + buildingName)as Sprite;
|
||
jin.sprite = jin1;
|
||
jin2.sprite = jin1;
|
||
imageHD(jin1, jin2);
|
||
Sprite weizhi = Resources.Load<Sprite>("noModels/weizhi/" + buildingName);
|
||
wz.sprite = weizhi;
|
||
imageHD(weizhi, wz);
|
||
Sprite Name = Resources.Load<Sprite>("noModels/name/" + buildingName);
|
||
noMN.sprite = Name;
|
||
noMN.preserveAspect = true;
|
||
Debug.Log("noModels/name/" + buildingName);
|
||
// Debug.Log(jin1+"-----"+Name+"-----"+noMN.sprite.name);
|
||
jin.DOFade(0, 2).From();
|
||
jin.transform.DOLocalMoveY((gu.rectTransform.rect.height) + 120, 2).From();
|
||
zi.DOFade(0, 2).From();
|
||
Content.childForceExpandWidth = false;
|
||
}
|
||
public void findB20()
|
||
{
|
||
disBuilding.SetActive(false);
|
||
Camera.GetComponent<CinemachineBrain>().enabled = true;
|
||
Camera.GetComponent<mouseMove>().enabled = false;
|
||
buildingTarget = findBuildingG.transform.Find(buildingName);
|
||
vcamA.SetActive(true);
|
||
vcam.SetActive(false);
|
||
}
|
||
void imageHD(Sprite sprite, Image image)
|
||
{
|
||
float targetWidth = 920;
|
||
float widthRatio = sprite.rect.width / sprite.rect.height;
|
||
float targetHeight = targetWidth / widthRatio;
|
||
Debug.Log(widthRatio);
|
||
image.sprite = sprite;
|
||
image.rectTransform.sizeDelta = new Vector2(targetWidth, targetHeight);
|
||
}
|
||
public void XXback()
|
||
{
|
||
Content.childForceExpandWidth = true;
|
||
XX.GetComponent<Animator>().Play("back");
|
||
//Content.transform.DORestart();
|
||
float delayTime = 1f; // 延时2秒后执行回调函数
|
||
DOTween.To(() => 0, x => { }, 0, delayTime)
|
||
.OnComplete(() =>
|
||
{
|
||
zi.text = null;
|
||
});
|
||
//gu.sprite = null;
|
||
//jin.sprite = null;
|
||
//wz.sprite = null;
|
||
//zi.text = null;
|
||
}
|
||
|
||
public void reTaget()
|
||
{
|
||
if (bd == 1)
|
||
{
|
||
disBuilding.SetActive(false);
|
||
Camera.GetComponent<mouseMove>().enabled = false;
|
||
Camera.GetComponent<CinemachineBrain>().enabled = true;
|
||
bd = 0;
|
||
}
|
||
else
|
||
{
|
||
|
||
}
|
||
}//可以删掉
|
||
|
||
public void onClick(GameObject temp)//为了方便回到场景,加的虚拟点击,可以不用管
|
||
{
|
||
temp.GetComponent<Button>().onClick.Invoke();
|
||
}
|
||
|
||
} |