[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

http://www.cnblogs.com/foreveryt/p/4228492.html

需要搞清楚:

1.本地仓库与远程仓库的同步(Sync)

2.版本回退(shell命令)

3.分支与合并(Pull request与merge)


八.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


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值