漫游脚本

以前做房地产的时候需要将100张图片根据手指滑动,来切换图片。今天保存,以备以后参考

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;
    }

  
	
}


转载于:https://my.oschina.net/u/698044/blog/615431

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 AI 漫游行走脚本,一般可以采用以下步骤: 1. 创建一个 AI 控制器对象,挂载一个 AI 控制器脚本。 2. 在 AI 控制器脚本中,定义一个目标点列表,用于存储 AI 行走的目标点。 3. 在 Start 函数中,初始化目标点列表,可以手动指定一些目标点,也可以在场景中寻找一些目标点。 4. 定义一个 MoveTo 函数,用于控制 AI 移动到目标点。 5. 在 Update 函数中,判断 AI 是否已经到达当前目标点,如果到达了,就切换到下一个目标点,并调用 MoveTo 函数。 6. 在 AI 控制器脚本中,可以设置 AI 的移动速度、旋转速度等参数。 下面是一个简单的示例代码: ```csharp using UnityEngine; using System.Collections.Generic; public class AIController : MonoBehaviour { public float moveSpeed = 5f; // AI 移动速度 public float rotateSpeed = 3f; // AI 旋转速度 public List<Transform> targetPoints; // 目标点列表 private int currentTargetIndex = 0; // 当前目标点索引 void Start() { // 初始化目标点列表 targetPoints = new List<Transform>(); GameObject[] points = GameObject.FindGameObjectsWithTag("TargetPoint"); foreach (GameObject point in points) { targetPoints.Add(point.transform); } // 移动到第一个目标点 MoveTo(targetPoints[currentTargetIndex]); } void Update() { // 判断是否到达当前目标点 if (Vector3.Distance(transform.position, targetPoints[currentTargetIndex].position) < 0.1f) { // 切换到下一个目标点 currentTargetIndex = (currentTargetIndex + 1) % targetPoints.Count; // 移动到下一个目标点 MoveTo(targetPoints[currentTargetIndex]); } } void MoveTo(Transform target) { // 计算目标方向 Vector3 targetDirection = target.position - transform.position; targetDirection.y = 0f; // 忽略高度差 targetDirection.Normalize(); // 计算旋转角度 float angle = Vector3.Angle(transform.forward, targetDirection); if (angle > 5f) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), rotateSpeed * Time.deltaTime); } // 控制移动 transform.position += transform.forward * moveSpeed * Time.deltaTime; } } ``` 在上述代码中,我们使用了一个 targetPoints 列表来存储 AI 行走的目标点,我们可以通过手动指定或者在场景中寻找来初始化。 在 Update 函数中,我们判断 AI 是否已经到达当前目标点,如果到达了,就切换到下一个目标点,并调用 MoveTo 函数,让 AI 移动到下一个目标点。 在 MoveTo 函数中,我们首先计算目标方向,然后计算旋转角度和移动控制。通过控制 AI 的旋转和移动,来实现 AI 漫游行走的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值