您需要 登录 才可以下载或查看,没有账号?注册
x
本帖最后由 悦动的喵 于 2019-3-23 15:31 编辑
unity3d读取文件(读取资源包中的文件,读取txt文件中的内容),滚动条,滚动字幕。
using UnityEngine;
using System.Collections;
using System;
public class MainGUI : MonoBehaviour {
// 放大 缩小 滚动ui
public GUISkin windowSkin;
private float screenWidth = Screen.width;
private float screenHeight = Screen.height;
//项目名
public string nameLabel;
private float nameLabelWidth = 200;
private float nameLabelHeight = 50;
//工具按钮
public GUIStyle[] toolBtnStyle;
private float toolBtnWidth = 110;
private float toolBtnHeight = 120;
private Vector2 scrollPosition;
private float scrollWidth = 1200;
//
public GameObject targetObj;
public GameObject[] qianbiPrefabs;
private GameObject qianbiObj;
public string[] jieshaoStrs;
private string jieshaoStr;
private float weicha;
private float jieshaoWidth = 2400;
void Start () {
//初始化滚动条位置
scrollPosition [0] = 5f;
scrollPosition [1] = 5f;
//资源初始化
for(int i = 0; i< toolBtnStyle.Length;i++){
//底部小图标
string icoTextureString = "Assets/UI/先秦钱币/B000" +(i+1) + ".png";
Texture2D icoTexture = Resources.LoadAssetAtPath(icoTextureString,typeof
(Texture2D)) as Texture2D;
toolBtnStyle .normal.background = icoTexture;
//prefab 要旋转的模型
string qianbiPrefabString = "Assets/Models/XIANQING/B000" +(i+1) + ".prefab";
GameObject qianbiPrefab = Resources.LoadAssetAtPath(qianbiPrefabString,typeof
(GameObject)) as GameObject;
qianbiPrefabs = qianbiPrefab;
//文字介绍
string jieshaoString = "Assets/Models/XIANQING/B000" +(i+1) + ".txt";
TextAsset jieshaoTextAsset = Resources.LoadAssetAtPath(jieshaoString,typeof
(TextAsset)) as TextAsset;
if(jieshaoTextAsset == null){
jieshaoStrs ="";
}else{
jieshaoStrs = jieshaoTextAsset.text;
}
}
//显示第一个
qianbiObj = Instantiate(qianbiPrefabs
[0],targetObj.transform.position,targetObj.transform.localRotation) as GameObject;
qianbiObj.AddComponent("MouseRotate");
jieshaoStr = jieshaoStrs[0];
}
// Update is called once per frame
void Update () {
weicha -= 50 * Time.deltaTime;
if(weicha<-jieshaoWidth){
weicha = 0;
}
}
void OnGUI(){
GUI.skin = windowSkin;
GUI.Label (new Rect((screenWidth-
nameLabelWidth)/2,0,nameLabelWidth,nameLabelHeight),nameLabel);
//滚动条 控制实现工具
scrollWidth = (toolBtnWidth + 5) * toolBtnStyle.Length;
if(scrollWidth < screenWidth-100){
scrollWidth = screenWidth-100;
}
scrollPosition = GUI.BeginScrollView(new Rect(50,screenHeight-150,screenWidth-
100,135),scrollPosition,new Rect(0,0,scrollWidth,50),false,false);
for (int i=0; i<toolBtnStyle.Length; i++) {
if(GUI.Button(new Rect((toolBtnWidth+5)
*i,5,toolBtnWidth,toolBtnHeight),"",toolBtnStyle)){
try{
weicha = 0;
Destroy(qianbiObj);
qianbiObj = Instantiate(qianbiPrefabs
,targetObj.transform.position,targetObj.transform.localRotation) as GameObject;
qianbiObj.AddComponent("MouseRotate");
jieshaoStr = jieshaoStrs;
Debug.Log(jieshaoStr.Length);
jieshaoWidth = jieshaoStr.Length * 15;
}catch(Exception ex){
Debug.Log(ex.Message);
}
//
}
}
GUI.EndScrollView();
GUI.Label (new Rect(50+weicha,screenHeight-200,jieshaoWidth,80),jieshaoStr);
}
} |