之前作房地產的時候須要將100張圖片根據手指滑動,來切換圖片。今天保存,以備之後參考c#
using UnityEngine; using System.Collections; public class Manyou : MonoBehaviour { public string m_CurrentPath = string.Empty; public float speed = 0.1f; public float AutoSpeed = 1; public int maxNum = 300; public int currentIndex = 0; public bool IsMveX= true; public bool IsStartFadeAnimation = false; // public manyouButton button; public UITexture swapGui; public UITexture narrow; //public bool AutoPlay = false; public bool canPlay = true; public GameObject Fadein; public GameObject FadeOut; public delegate void RestEvent(); public RestEvent rest; public bool LoadFromStreamAssect=false; float temp = 0; public bool AutoPlay = false; public string narrowsPath = "Map/"; public void InitFirstImage() { currentIndex = 0; string path = string.Format(m_CurrentPath, 0); Texture text = (Texture)Resources.Load(path) as Texture; Material mater = new Material(Shader.Find("Unlit/Transparent Colored")); Destroy (mater .mainTexture); mater.mainTexture = text; swapGui.material = mater; Resources.UnloadUnusedAssets(); if(narrow != null) StartCoroutine(loadMap(0)); } public void InitData(string path, int Length) { currentIndex = 0; this.m_CurrentPath = path; this.maxNum = Length; InitFirstImage(); } void Update() { if (enabled && canPlay && AutoPlay) { temp+=AutoSpeed; if (temp >= 1) { currentIndex++; swapImg(Vector2.zero); temp = 0; } } } void OnEnable() { FingerGestures.OnLongPress += OnLongPress; FingerGestures.OnDragMove += OnDragMove; FingerGestures.OnFingerUp += OnFingerUp; //WinGesture.Instance.OnDragMove+=OnDragMove; } void OnDisable() { FingerGestures.OnLongPress -= OnLongPress; FingerGestures.OnFingerUp -= OnFingerUp; FingerGestures.OnDragMove -= OnDragMove; } void OnFingerUp(int fingerIndex, Vector2 fingerPos, float timeHeldDown) { AutoPlay = false; } void OnLongPress(Vector2 fingerPos) { AutoPlay = true; } void OnDragMove(Vector2 fingerPos, Vector2 delta) { if (canPlay) swapImg(delta); } void swapImg(Vector2 delta) { if(IsMveX){ Debug.Log(currentIndex +" "+(int)(delta.x * speed)); currentIndex -= (int)(delta.x * speed); }else{ Debug.Log(currentIndex +" "+((int)(delta.y * speed)) + " " + delta.x+ " " + speed); currentIndex -= (int)(delta.y * speed); Debug.Log("CurrentIndex="+currentIndex); } if (currentIndex > maxNum) { if(IsStartFadeAnimation && AutoPlay) { MyClamp(); if(rest != null) rest(); return; } currentIndex = 0; } if (currentIndex < 0) currentIndex = maxNum; if (LoadFromStreamAssect) { StartCoroutine(LoadImg()); return ; } string path = string.Format(m_CurrentPath, currentIndex); Texture text = (Texture)Resources.Load(path) as Texture; Material mater = new Material(Shader.Find("Unlit/Transparent Colored")); Destroy (mater .mainTexture); mater.mainTexture = text; swapGui.material = mater; Resources.UnloadUnusedAssets(); if(narrow != null) StartCoroutine(loadMap(currentIndex)); } void OnGUI() { if(GUI.Button(new Rect(10,10,100,50), "Add")) { AutoSpeed += 0.1f; } else if(GUI.Button(new Rect(10,70,100,50), "Subtract")) { AutoSpeed -= 0.1f; } GUI.color = Color.red; GUI.Label(new Rect(10,150,100,50),AutoSpeed+""); } IEnumerator loadMap(int index) { string path = string.Format(narrowsPath + m_CurrentPath, index); WWW www =new WWW("file://"+Application.streamingAssetsPath+"/"+path+".png"); yield return www ; Material mater = new Material(Shader.Find("Unlit/Transparent Colored")); Destroy (mater .mainTexture); mater.mainTexture = www.texture; narrow.material = mater; canPlay =true; Resources.UnloadUnusedAssets(); } WWW www; IEnumerator LoadImg() { canPlay=false; string path=string.Format(m_CurrentPath,currentIndex); Debug.Log(currentIndex); www =new WWW("file://"+Application.streamingAssetsPath+"/"+path+".jpg"); yield return www ; Material mater = new Material(Shader.Find("Unlit/Transparent Colored")); Destroy (mater .mainTexture); mater.mainTexture = www.texture; swapGui.material = mater; canPlay =true; Resources.UnloadUnusedAssets(); } void MyClamp() { if (currentIndex <= 0) //currentIndex = 0; currentIndex = maxNum; if (currentIndex > maxNum) { canPlay = false; StartCoroutine(Fade(1f)); } } public void Reset() { AutoPlay = false; StartCoroutine(Fade(1f)); } public void AutoRun() { AutoPlay = true; } IEnumerator Fade(float timer) { GameObject fadein= Instantiate(FadeOut) as GameObject; string path=null; Material mater; currentIndex = 0; if (LoadFromStreamAssect) { path=string.Format(m_CurrentPath,currentIndex); Debug.Log(currentIndex); www =new WWW("file://"+Application.streamingAssetsPath+"/"+path+".jpg"); yield return www ; mater = new Material(Shader.Find("Unlit/Transparent Colored")); Destroy (mater .mainTexture); mater.mainTexture = www.texture; swapGui.material = mater; canPlay =true; Destroy(fadein); Resources.UnloadUnusedAssets(); yield break; } path = string.Format(m_CurrentPath, currentIndex); Texture text = (Texture)Resources.Load(path) as Texture; mater = new Material(Shader.Find("Unlit/Transparent Colored")); mater.mainTexture = text; swapGui.material = mater; yield return new WaitForSeconds(timer); Destroy(fadein); canPlay = true; AutoPlay = false; } }