纳金网

标题: 行为类的交互脚本 [打印本页]

作者: 狂风大尉    时间: 2014-6-25 01:13
标题: 行为类的交互脚本
  1.  using UnityEngine;

  2.   using System.Collections;

  3.   using System.IO;

  4.   using System.Xml;

  5.   public class Book : MonoBehaviour {

  6.   // Use this for initialization

  7.   string _FileLocation;

  8.   string _FileName = “MConfig.xml”;

  9.   string _xmlData;

  10.   XmlDocument configData;

  11.   private ArrayList pages;

  12.   public GameObject page;

  13.   private int index = 0;

  14.   private int pageLen = 0;

  15.   void Start () {

  16.   _FileLocation = Application.dataPath;

  17.   pages = new ArrayList();

  18.   LoadXML();

  19.   initBook();

  20.   }

  21.   // Update is called once per frame

  22.   void Update () {

  23.   }

  24.   void LoadXML() {

  25.   StreamReader r = File.OpenText(_FileLocation + “\\” + _FileName);

  26.   string info = r.ReadToEnd();

  27.   r.Close();

  28.   _xmlData = info;

  29.   configData = new XmlDocument();

  30.   configData.LoadXml(_xmlData);

  31.   }

  32.   void initBook() {

  33.   foreach (XmlNode item in configData.SelectNodes(“/mConfig/page”))

  34.   {

  35.   //print(item.Attributes[“murl”].Value);材质路径

  36.   GameObject obj = (GameObject)Instantiate(page);

  37.   obj.renderer.material.mainTexture = (Texture)Resources.Load(item.Attributes[“murl”].Value);

  38.   pages.Add(obj);

  39.   pageLen++;

  40.   }

  41.   }

  42.   public void PageUp(){

  43.   print(“page Up”);

  44.   }

  45.   public void PageDown() {

  46.   print(“page Down”);

  47.   }

  48.   }
复制代码
这里我们用了加载XML的方式来生成指定的书的页,那么我们如何才能控制PageUp()以及PageDown()方法呢?具体的方式是通过如下代码:
  1.  using UnityEngine;

  2.   using System.Collections;

  3.   public class NButton : MonoBehaviour {

  4.   // Use this for initialization

  5.   private Book book;

  6.   void Start () {

  7.   }  Unity3D教程手册

  8.   // Update is called once per frame

  9.   void Update () {

  10.   }

  11.   void OnMouseDown() {

  12.   book = (Book)GameObject.Find(“Book”).GetComponent(typeof(Book));

  13.   book.PageDown();

  14.   }

  15.   }
复制代码
原文来自:
     Unity3D教程手册:http://www.unitymanual.com/6220.html

作者: hyui    时间: 2014-6-25 02:35
Thanks for sharing this!
作者: wucnj    时间: 2014-6-25 09:06
感谢分享!!!
作者: hariboot    时间: 2014-6-25 09:12
MARK THIS CONTENT




欢迎光临 纳金网 (http://rs.narkii.com/club/) Powered by Discuz! X2.5