框架視圖
關鍵代碼
AnimaController
public class AnimaController : MonoBehaviour {
//為了獲取自身的動畫的變量
protected Animator welcomeAnim;
//為了獲取攝像機的動畫公開的變量;
public Animator cameraAnim;
//定義一個開關是否啟動開始播放動畫;
private bool isWelcomeStart=false;
// Use this for initialization
void Start () {
//自身動畫賦值;
welcomeAnim=gameObject.transform.GetComponent<Animator>();
//一開始都不要播放;
welcomeAnim.enabled=false;
cameraAnim.enabled = false;
}
// Update is called once per frame
void Update () {
//時間過了2秒后 并且開關是處于關閉的狀態下 開始播放動畫
if (Time.time>2&&!isWelcomeStart) {
isWelcomeStart = true;
welcomeAnim.enabled = true;
}
}
/// <summary>
///播放放文字動畫后 啟動播放攝像機動畫的事件;
/// </summary>
public void enableCameraAnim(){
cameraAnim.enabled = true;
}
}
GalleryData
public class GalleryData {
//此腳本主要是用來顯示關鍵值得;
private string image;
private string description;
private string title;
//設置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 {
//聲明一個公開的變量持有相機的應用;
public CharacterController controller;
//定義向前移動或向后移動的開關;
private bool isforward;
private bool isbackward;
//定義一個初始化位置;
private Vector3 initialPosition;
//UI界面
//定義點擊油畫的游戲對象 菜單節點
public GameObject descriptionMenu;
//定義公開的相片
public Image image;
//公開標題的屬性
public Text title;
//定義公開描述的文本;
public Text description;
//播放完表示是否恩能夠移動
private bool canMove;
//攝像機動畫
private Animator cameraAnim;
//文本動畫;
private Animator welcomeAnim;
void Awake(){
//開啟協程
StartCoroutine (JsonLoader.LoadPaints ());
}
void Start () {
//油畫介紹UI一開始隱藏界面
descriptionMenu.SetActive(false);
//初始化時候設置為false;
isforward = false;
isbackward = false;
//開始時游戲不能控制 播放完畢后可以控制
controller.enabled=false;
canMove = false;
//對兩個動畫進行賦值;
cameraAnim=GameObject.Find("Main Camera").GetComponent<Animator>();
welcomeAnim = GameObject.Find ("WelcomeGreeting").GetComponent<Animator> ();
}
void Update () {
//如果可移動且可以控制,擦能作一下操作;
if (canMove&&controller==true) {
detectMoving ();
//初始化位置為0;
Vector3 axis = Vector3.zero;
//輸入向上的箭頭
if (Input.GetKey(KeyCode.UpArrow)||isforward) {
//向前移動
axis = Camera.main.transform.forward;
}else if (Input.GetKey(KeyCode.DownArrow)||isbackward) {
axis = -Camera.main.transform.forward;
}else if (Input.GetKey(KeyCode.LeftArrow)) {
//定義旋轉的變量
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);
}
//角色移動控制;
controller.SimpleMove (axis*3.0f);
}
//在update函數中,如果點擊鼠標 就把介紹油畫的Ui顯示出來;
if (Input.GetMouseButtonDown(0)) {
//如果是顯示狀態,就把介紹UI隱藏起來;
if (descriptionMenu.activeSelf==true) {
descriptionMenu.SetActive(false);
return;
}
//否則點擊油畫即顯示UI介紹的界面
//定義一個從攝像機向前的方向;
Vector3 direction = Camera.main.transform.forward;
//定義存貯的碰撞信息
RaycastHit hit;
//定義一條射線
Ray cameraRay=new Ray(Camera.main.transform.position,direction);
//發出射線;
Physics.Raycast(cameraRay,out hit,100);//參數1:射線 參數2:射線方向 參數3:射進去100;
//如果射線碰撞到的是frame,就顯示UI油畫介紹界面;
if (hit.collider.gameObject.tag=="frame") {
//通過json類下的方法查找(利用碰撞到的名字去查)
GalleryData gdata=JsonLoader.FindGalleryinJson(hit.collider.gameObject.name);
//圖片進行賦值;
image.sprite=Resources.Load("Sprites/"+gdata.getTitle(),typeof(Sprite)) as Sprite;
//標題的名字;
title.text=gdata.getTitle();
//描述
description.text = gdata.getDescription();
//把菜單顯示出來;
descriptionMenu.SetActive(true);
}
}
}
/// <summary>
/// 移動的方法
/// </summary>
public void detectMoving(){
//鼠標按下;
if (Input.GetMouseButtonDown(0)) {
initialPosition = Input.mousePosition;
} else if (Input.GetMouseButton (0)) {//持續按下鼠標
//判斷鼠標移動的位置;
//當前鼠標的值減去初始化的值小于-40 判斷不是向后走就是向前走
if ((Input.mousePosition - initialPosition).x < -40) {
//判斷不是向前就是向后
if (!isbackward) {
isforward = true;
}
}
//當前鼠標位置-初始化位置的值大于40,判斷不是向前走就是向后走
if ((Input.mousePosition-initialPosition).x>40) {
if (!isforward) {
isbackward = true;
}
}
}//判斷鼠標抬起的時候
else if (Input.GetMouseButtonUp(0)){
//重新將開關 關閉
isforward=false;
isbackward = false;
}
}
/// <summary>
/// 播放完動畫后 動畫就不能使用,游戲開始可以控制;
/// </summary>
public void EnableMovement(){
//動畫停止;
welcomeAnim.enabled = false;
cameraAnim.enabled = false;
//游戲開始可以控制;
controller.enabled = true;
canMove = true;
}
}
JsonLoader
public class JsonLoader : MonoBehaviour {
//定義一個靜態的json對象
private static JSONObject gallerydata;
//協程
public static IEnumerator LoadPaints(){//載入油畫
//關鍵文件夾 載入 文件名 轉化成 textasset 對象
TextAsset asset = Resources.Load ("nanogallerydata") as TextAsset;
//靜態的,實例化對象
gallerydata = new JSONObject(asset.text);
//便利鍵值對;
foreach (string key in gallerydata.keys) {
//標題
string title = (gallerydata [key] ["title"]).str;
//材料
Material mat = Resources.Load ("Materials/"+title, typeof(Material)) as Material;
//通過“Find”方法查找鍵賦值給 新對象
GameObject frame = GameObject.Find (key);
//獲取相關組件下的材質屬性 進行賦值;
frame.GetComponent<MeshRenderer> ().material = mat;
//獲取子組件下第一個組件的文本屬性進行賦值;
frame.transform.GetChild (0).GetComponentInChildren<TextMesh> ().text = title;
}
//協程關鍵語句 返回空;
yield return null;
}
//定義一個公開的靜態的函數 通過key查找的方法
public static GalleryData FindGalleryinJson(string key){
//實例化該類
GalleryData gdata = new GalleryData ();
//獲取key下面的游戲對象;
JSONObject galleryJson = gallerydata [key];
//如果不是空 進行賦值;
if (galleryJson != null) {
gdata.setImage (galleryJson ["image"].str);
gdata.setDescription (galleryJson ["description"].str);
gdata.setTitle (galleryJson ["title"].str);
}
//把獲取的值進行返回
return gdata;
}
}