框架視圖
關(guān)鍵代碼
AnimaController
public class AnimaController : MonoBehaviour {
//為了獲取自身的動(dòng)畫的變量
protected Animator welcomeAnim;
//為了獲取攝像機(jī)的動(dòng)畫公開的變量;
public Animator cameraAnim;
//定義一個(gè)開關(guān)是否啟動(dòng)開始播放動(dòng)畫连霉;
private bool isWelcomeStart=false;
// Use this for initialization
void Start () {
//自身動(dòng)畫賦值榴芳;
welcomeAnim=gameObject.transform.GetComponent<Animator>();
//一開始都不要播放;
welcomeAnim.enabled=false;
cameraAnim.enabled = false;
}
// Update is called once per frame
void Update () {
//時(shí)間過了2秒后 并且開關(guān)是處于關(guān)閉的狀態(tài)下 開始播放動(dòng)畫
if (Time.time>2&&!isWelcomeStart) {
isWelcomeStart = true;
welcomeAnim.enabled = true;
}
}
/// <summary>
///播放放文字動(dòng)畫后 啟動(dòng)播放攝像機(jī)動(dòng)畫的事件跺撼;
/// </summary>
public void enableCameraAnim(){
cameraAnim.enabled = true;
}
}
GalleryData
public class GalleryData {
//此腳本主要是用來顯示關(guān)鍵值得窟感;
private string image;
private string description;
private string title;
//設(shè)置set 和 get屬性;
public void setImage(string image){
this.image = image;
}
public string getImage(){
return image;
}
public void setDescription(string description){
this.description = description;
}
public string getDescription(){
return description;
}
public void setTitle(string title){
this.title = title;
}
public string getTitle(){
return title;
}
}
gameController
public class gameController : MonoBehaviour {
//聲明一個(gè)公開的變量持有相機(jī)的應(yīng)用歉井;
public CharacterController controller;
//定義向前移動(dòng)或向后移動(dòng)的開關(guān)柿祈;
private bool isforward;
private bool isbackward;
//定義一個(gè)初始化位置;
private Vector3 initialPosition;
//UI界面
//定義點(diǎn)擊油畫的游戲?qū)ο?菜單節(jié)點(diǎn)
public GameObject descriptionMenu;
//定義公開的相片
public Image image;
//公開標(biāo)題的屬性
public Text title;
//定義公開描述的文本;
public Text description;
//播放完表示是否恩能夠移動(dòng)
private bool canMove;
//攝像機(jī)動(dòng)畫
private Animator cameraAnim;
//文本動(dòng)畫躏嚎;
private Animator welcomeAnim;
void Awake(){
//開啟協(xié)程
StartCoroutine (JsonLoader.LoadPaints ());
}
void Start () {
//油畫介紹UI一開始隱藏界面
descriptionMenu.SetActive(false);
//初始化時(shí)候設(shè)置為false蜜自;
isforward = false;
isbackward = false;
//開始時(shí)游戲不能控制 播放完畢后可以控制
controller.enabled=false;
canMove = false;
//對(duì)兩個(gè)動(dòng)畫進(jìn)行賦值;
cameraAnim=GameObject.Find("Main Camera").GetComponent<Animator>();
welcomeAnim = GameObject.Find ("WelcomeGreeting").GetComponent<Animator> ();
}
void Update () {
//如果可移動(dòng)且可以控制卢佣,擦能作一下操作重荠;
if (canMove&&controller==true) {
detectMoving ();
//初始化位置為0;
Vector3 axis = Vector3.zero;
//輸入向上的箭頭
if (Input.GetKey(KeyCode.UpArrow)||isforward) {
//向前移動(dòng)
axis = Camera.main.transform.forward;
}else if (Input.GetKey(KeyCode.DownArrow)||isbackward) {
axis = -Camera.main.transform.forward;
}else if (Input.GetKey(KeyCode.LeftArrow)) {
//定義旋轉(zhuǎn)的變量
Vector3 rotation = this.transform.rotation.eulerAngles;
rotation.y-=1;
rotation.y = rotation.y %360;
//重新賦值虚茶;
this.transform.rotation = Quaternion.Euler (rotation);
}else if (Input.GetKey(KeyCode.RightArrow)) {
Vector3 rotation = this.transform.rotation.eulerAngles;
rotation.y += 1;
rotation.y = rotation.y % 360;
this.transform.rotation = Quaternion.Euler (rotation);
}
//角色移動(dòng)控制戈鲁;
controller.SimpleMove (axis*3.0f);
}
//在update函數(shù)中,如果點(diǎn)擊鼠標(biāo) 就把介紹油畫的Ui顯示出來嘹叫;
if (Input.GetMouseButtonDown(0)) {
//如果是顯示狀態(tài)婆殿,就把介紹UI隱藏起來;
if (descriptionMenu.activeSelf==true) {
descriptionMenu.SetActive(false);
return;
}
//否則點(diǎn)擊油畫即顯示UI介紹的界面
//定義一個(gè)從攝像機(jī)向前的方向罩扇;
Vector3 direction = Camera.main.transform.forward;
//定義存貯的碰撞信息
RaycastHit hit;
//定義一條射線
Ray cameraRay=new Ray(Camera.main.transform.position,direction);
//發(fā)出射線鸣皂;
Physics.Raycast(cameraRay,out hit,100);//參數(shù)1:射線 參數(shù)2:射線方向 參數(shù)3:射進(jìn)去100;
//如果射線碰撞到的是frame暮蹂,就顯示UI油畫介紹界面寞缝;
if (hit.collider.gameObject.tag=="frame") {
//通過json類下的方法查找(利用碰撞到的名字去查)
GalleryData gdata=JsonLoader.FindGalleryinJson(hit.collider.gameObject.name);
//圖片進(jìn)行賦值;
image.sprite=Resources.Load("Sprites/"+gdata.getTitle(),typeof(Sprite)) as Sprite;
//標(biāo)題的名字仰泻;
title.text=gdata.getTitle();
//描述
description.text = gdata.getDescription();
//把菜單顯示出來荆陆;
descriptionMenu.SetActive(true);
}
}
}
/// <summary>
/// 移動(dòng)的方法
/// </summary>
public void detectMoving(){
//鼠標(biāo)按下;
if (Input.GetMouseButtonDown(0)) {
initialPosition = Input.mousePosition;
} else if (Input.GetMouseButton (0)) {//持續(xù)按下鼠標(biāo)
//判斷鼠標(biāo)移動(dòng)的位置集侯;
//當(dāng)前鼠標(biāo)的值減去初始化的值小于-40 判斷不是向后走就是向前走
if ((Input.mousePosition - initialPosition).x < -40) {
//判斷不是向前就是向后
if (!isbackward) {
isforward = true;
}
}
//當(dāng)前鼠標(biāo)位置-初始化位置的值大于40被啼,判斷不是向前走就是向后走
if ((Input.mousePosition-initialPosition).x>40) {
if (!isforward) {
isbackward = true;
}
}
}//判斷鼠標(biāo)抬起的時(shí)候
else if (Input.GetMouseButtonUp(0)){
//重新將開關(guān) 關(guān)閉
isforward=false;
isbackward = false;
}
}
/// <summary>
/// 播放完動(dòng)畫后 動(dòng)畫就不能使用,游戲開始可以控制棠枉;
/// </summary>
public void EnableMovement(){
//動(dòng)畫停止浓体;
welcomeAnim.enabled = false;
cameraAnim.enabled = false;
//游戲開始可以控制辈讶;
controller.enabled = true;
canMove = true;
}
}
JsonLoader
public class JsonLoader : MonoBehaviour {
//定義一個(gè)靜態(tài)的json對(duì)象
private static JSONObject gallerydata;
//協(xié)程
public static IEnumerator LoadPaints(){//載入油畫
//關(guān)鍵文件夾 載入 文件名 轉(zhuǎn)化成 textasset 對(duì)象
TextAsset asset = Resources.Load ("nanogallerydata") as TextAsset;
//靜態(tài)的命浴,實(shí)例化對(duì)象
gallerydata = new JSONObject(asset.text);
//便利鍵值對(duì);
foreach (string key in gallerydata.keys) {
//標(biāo)題
string title = (gallerydata [key] ["title"]).str;
//材料
Material mat = Resources.Load ("Materials/"+title, typeof(Material)) as Material;
//通過“Find”方法查找鍵賦值給 新對(duì)象
GameObject frame = GameObject.Find (key);
//獲取相關(guān)組件下的材質(zhì)屬性 進(jìn)行賦值贱除;
frame.GetComponent<MeshRenderer> ().material = mat;
//獲取子組件下第一個(gè)組件的文本屬性進(jìn)行賦值生闲;
frame.transform.GetChild (0).GetComponentInChildren<TextMesh> ().text = title;
}
//協(xié)程關(guān)鍵語(yǔ)句 返回空;
yield return null;
}
//定義一個(gè)公開的靜態(tài)的函數(shù) 通過key查找的方法
public static GalleryData FindGalleryinJson(string key){
//實(shí)例化該類
GalleryData gdata = new GalleryData ();
//獲取key下面的游戲?qū)ο螅? JSONObject galleryJson = gallerydata [key];
//如果不是空 進(jìn)行賦值月幌;
if (galleryJson != null) {
gdata.setImage (galleryJson ["image"].str);
gdata.setDescription (galleryJson ["description"].str);
gdata.setTitle (galleryJson ["title"].str);
}
//把獲取的值進(jìn)行返回
return gdata;
}
}