458 lines
14 KiB
C#
458 lines
14 KiB
C#
using System.Collections;
|
||
using UnityEngine.SceneManagement;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using System.IO;
|
||
using System;
|
||
using TMPro;
|
||
using UnityEngine.Networking;
|
||
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 AudioSource AS;
|
||
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;
|
||
}
|
||
}
|
||
//AS.clip = Resources.Load<AudioClip>("sound/" + buildingName);
|
||
|
||
}
|
||
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 findBuildingV()
|
||
{
|
||
if (bd == 1)
|
||
{
|
||
disBuilding.SetActive(false);
|
||
Debug.Log("--------");
|
||
}
|
||
if (bd == 1 && buildingTarget.name == buildingName)
|
||
{
|
||
ToScene("buildingV");
|
||
}
|
||
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 findBuildingVInvoke(float a)
|
||
{
|
||
Invoke("findBuildingV",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);
|
||
string g = "noModels/gu/";
|
||
string j = "noModels/jin/";
|
||
string w = "noModels/weizhi/";
|
||
string n = "noModels/name/";
|
||
Sprite spriteName = loadSpriteFromSA(buildingName,g);
|
||
gu.sprite = spriteName;
|
||
imageHD(spriteName, gu);
|
||
//Sprite jin1 = Resources.Load<Sprite>("noModels/jin/" + buildingName)as Sprite;
|
||
Sprite jin1 = loadSpriteFromSA(buildingName, j);
|
||
jin.sprite = jin1;
|
||
jin2.sprite = jin1;
|
||
imageHD(jin1, jin2);
|
||
//Sprite weizhi = Resources.Load<Sprite>("noModels/weizhi/" + buildingName);
|
||
Sprite weizhi = loadSpriteFromSA(buildingName,w);
|
||
wz.sprite = weizhi;
|
||
imageHD(weizhi, wz);
|
||
//Sprite Name = Resources.Load<Sprite>("noModels/name/" + buildingName);
|
||
Sprite Name = loadSpriteFromSA(buildingName, n);
|
||
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;
|
||
//AS.clip = Resources.Load<AudioClip>("sound/" + buildingName);
|
||
loadSound();
|
||
}
|
||
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 Sprite loadSpriteFromSA(string buildingName, string ML)
|
||
{
|
||
// 构建完整的文件路径,将文件名和StreamingAssets下的具体文件夹路径拼接起来
|
||
string filePath = Path.Combine(Application.streamingAssetsPath, ML + buildingName + ".png");
|
||
|
||
// 读取文件的字节数据
|
||
byte[] fileData = File.ReadAllBytes(filePath);
|
||
|
||
// 创建一个Texture2D对象,用于后续加载图片数据
|
||
Texture2D texture = new Texture2D(2, 2);
|
||
|
||
// 从字节数据中加载图片信息到Texture2D对象,如果加载成功
|
||
if (texture.LoadImage(fileData))
|
||
{
|
||
// 根据加载好的Texture2D创建Sprite对象
|
||
return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
|
||
}
|
||
|
||
// 如果加载失败,返回null
|
||
return null;
|
||
}
|
||
public void LoadSoundFromSA(string buildingName, string ML, Action<AudioClip> onAudioLoaded)
|
||
{
|
||
// 构建完整的文件路径
|
||
string filePath = Path.Combine(Application.streamingAssetsPath, ML, buildingName + ".wav");
|
||
|
||
// 判断文件是否存在
|
||
if (!File.Exists(filePath))
|
||
{
|
||
Debug.LogError($"音频文件 {filePath} 不存在!");
|
||
onAudioLoaded(null); // 回调并传递null
|
||
return;
|
||
}
|
||
|
||
// 启动协程加载音频
|
||
StartCoroutine(LoadAudioClipCoroutine(filePath, onAudioLoaded));
|
||
}
|
||
|
||
private IEnumerator LoadAudioClipCoroutine(string filePath, Action<AudioClip> onAudioLoaded)
|
||
{
|
||
// 创建UnityWebRequest来加载音频
|
||
UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file://" + filePath, AudioType.WAV);
|
||
yield return www.SendWebRequest();
|
||
|
||
// 检查加载是否成功
|
||
if (www.result != UnityWebRequest.Result.Success)
|
||
{
|
||
Debug.LogError($"音频加载失败: {www.error}");
|
||
onAudioLoaded(null); // 回调并传递null
|
||
}
|
||
else
|
||
{
|
||
// 获取并返回加载的音频
|
||
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(www);
|
||
onAudioLoaded(audioClip); // 回调并传递加载的音频
|
||
Debug.Log("音频加载成功:" + filePath);
|
||
|
||
}
|
||
}
|
||
|
||
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();
|
||
}
|
||
|
||
public void loadSound()
|
||
{
|
||
// 调用加载音频的方法,并传递回调函数
|
||
this.LoadSoundFromSA(buildingName, "sound/", (audioClip) =>
|
||
{
|
||
if (audioClip != null)
|
||
{
|
||
Debug.Log("音频加载成功,"+ buildingName + "播放音频!");
|
||
AS.clip = audioClip;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("音频加载失败!");
|
||
}
|
||
});
|
||
}
|
||
public void soundPlay(AudioSource A)
|
||
{
|
||
if (A.isPlaying)
|
||
{
|
||
A.Stop();
|
||
A.GetComponent<Animator>().Play("stop");
|
||
}
|
||
else
|
||
{
|
||
A.Play();
|
||
A.GetComponent<Animator>().Play("play");
|
||
}
|
||
}
|
||
|
||
} |