[Unity技巧]一些小技巧和小脚本

一.让Sprite接受灯光影响

1. Assets->Create->Meterial ,并修改Shader为Sprite/Diffuse
2. 选中2DSprite,修改其Sprite Renderer的Material为1步中创建的Meterial
3. 拖动Point light到合适位置



二.让物体围绕自身某一点旋转

1.建立一个空物体,然后将空物体的中心拖动到需要旋转的物体的旋转点

2.然后将需要旋转的物体拖拽给此空物体,成为空物体的子物体

3.使空物体旋转


三.y轴上的循环滚动

using UnityEngine;
using System.Collections;

//Wrap Mode改为Repeat
public class RoadMove : MonoBehaviour {

    public float speed = 1f;
    private MeshRenderer mr;

    void Start()
    {
        mr = GetComponent<MeshRenderer>();
    }

	// Update is called once per frame
	void Update () 
    {
        Vector2 offset = new Vector2(0,Time.time * speed);
        mr.material.mainTextureOffset = offset;
	}
}

四.摄像机震动

using UnityEngine;
using System.Collections;

//也可以使用AnimationCurve
public class CameraShake : MonoBehaviour {

    public float strength = 0.5f;//震动幅度
    public float rate = 35f;//震动频率
    public float shakeTime = 0.05f;//震动时长

    private bool isShake = false;
    private float timer = 0f;
    private Vector3 recordPos;

    public void Shake(float strength, float rate, float shakeTime)
    {
        this.strength = strength;
        this.rate = rate;
        this.shakeTime = shakeTime;

        if (!isShake)
        {
            isShake = true;
            timer = 0f;
            recordPos = transform.localPosition;
            StartCoroutine(ShakeCamera());
        }
    }
    
    IEnumerator ShakeCamera()
    {
        while (timer < shakeTime)
        {
            timer += Time.deltaTime;
            transform.localPosition = recordPos + new Vector3(Mathf.Sin(rate * timer), -Mathf.Cos(rate * timer), 0) * Mathf.Lerp(strength, 0, timer);
            yield return null;
        }
        isShake = false;
        transform.localPosition = recordPos;
    }
}

五.进度条加载

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

//参考链接:http://blog.csdn.net/ldghd/article/details/47258045
public class LoadingScene : MonoBehaviour {

    private Image image;
    private Text text;

    void Start() 
    {
        image = GetComponent<Image>();
        text = transform.FindChild("Text").GetComponent<Text>();
        StartCoroutine(LoadScene("Forest"));
	}

    IEnumerator LoadScene(string sceneName)
    {
        int displayProgress = 0;
        int toProgress = 0;
        AsyncOperation asyncOperation = Application.LoadLevelAsync(sceneName);
        asyncOperation.allowSceneActivation = false;

        while (asyncOperation.progress < 0.9f)
        {
            toProgress = (int)(asyncOperation.progress * 100);
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                SetLoadingPercentage(displayProgress);
                yield return new WaitForEndOfFrame();
            }
            yield return new WaitForEndOfFrame();
        }

        toProgress = 100;
        while (displayProgress < toProgress)
        {
            ++displayProgress;
            SetLoadingPercentage(displayProgress);
            yield return new WaitForEndOfFrame();
        }
        asyncOperation.allowSceneActivation = true;
    }

    void SetLoadingPercentage(int percentage)
    {
        image.fillAmount = (float)percentage / 100;
        text.text = percentage + "%";
    }
}


六.使用disunity提取游戏资源

http://www.cr173.com/soft/97114.html

http://blog.csdn.net/qinyuanpei/article/details/45169857


七.GitHub for Window使用教程

http://www.cnblogs.com/jiqing9006/p/3987702.html


八.UGUI制作的摇杆

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class Joystick : EventTrigger {

    public float maxDis = 30;
    private Vector2 direction;
    private Vector2 originPos;
    private Move playerMove;

	void Start () 
    {
        originPos = transform.position;
        playerMove = GameObject.FindGameObjectWithTag("Player").GetComponent<Move>();
	}

    public override void OnDrag(PointerEventData eventData)
    {
        direction = Vector3.Normalize(eventData.position - originPos);

        if (Vector3.Distance(eventData.position, originPos) < maxDis)
            transform.position = eventData.position;
        else
            transform.position = originPos + direction * maxDis;

        playerMove.SetDir(direction);
    }

    public override void OnEndDrag(PointerEventData eventData)
    {
        direction = Vector2.zero;
        transform.position = originPos;
        playerMove.SetDir(direction);
    }
}


九.设置脚本模板头

找到Editor\Data\Resources\ScriptTemplates,按照格式新建一个text,例如为:87-My C# Script-NewBehaviourScript.cs.txt,内容为:

using UnityEngine;
using System.Collections;

/*
功能描述:#FunctionDescription#
作者:#AuthorName#
时间:#CreateTime#
*/
public class #SCRIPTNAME# : MonoBehaviour {

	void Start () 
	{
	}
	
	void Update () 
	{
	}

}

然后编写编辑器脚本:

using UnityEngine;
using System.Collections;
using System.IO;
using UnityEditor;
using System;

public class SetScriptHead : AssetModificationProcessor {

    public static void OnWillCreateAsset(string path)
    {
        path = path.Replace(".meta", "");
        if (path.EndsWith(".cs"))
        {
            string allText = File.ReadAllText(path);
            allText = allText.Replace("#FunctionDescription#", "")
                .Replace("#AuthorName#", "lyh")
                .Replace("#CreateTime#", DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day);
            File.WriteAllText(path, allText);
        }
    }

}


十.

动态阴影:

3D:http://www.xuanyusong.com/archives/2132

使用RenderTexture制作阴影,此外可以把RenderTexture赋值给RawImage,从而实现在UI前显示模型

2D:http://blog.csdn.net/mutou_222/article/details/48003277#comments


你可能感兴趣的:(小技巧)