存档

2015年6月 的存档

分别修改Cube每个面的贴图UV(Unity3D开发之十八)

2015年6月23日 没有评论

猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢!
原文地址: http://blog.csdn.net/cocos2der/article/details/46611169

今天项目中需要修改一个Cube中每个面的贴图UV,也就是贴图中有多个矩形贴图,需要程序从贴图中读取一部分赋值给Cube每个面。

这里已经有人实现了。

CustomUVS.cs

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class CustomUVS : MonoBehaviour {
    public Vector2 topPoint;
    public Vector2 bottomPoint;
    public Vector2 leftPoint;
    public Vector2 rightPoint;
    public Vector2 frontPoint;
    public Vector2 backPoint;

    private Mesh m_mesh;
    public enum CubeFaceType
    {
        Top,
        Bottom,
        Left,
        Right,
        Front,
        Back
    };

    // Use this for initialization
    void Start () {
        MeshFilter meshFilter = GetComponent<MeshFilter>();
        if (meshFilter == null) {
            Debug.LogError("Script needs MeshFilter component");
            return;
        }

#if UNITY_EDITOR
        Mesh meshCopy = Mesh.Instantiate(meshFilter.sharedMesh) as Mesh;    // Make a deep copy
        meshCopy.name = "Cube";
        m_mesh = meshFilter.mesh = meshCopy;                                // Assign the copy to the meshes
#else
        m_mesh = meshFilter.mesh;
#endif
        if (m_mesh == null || m_mesh.uv.Length != 24) {
            Debug.LogError("Script needs to be attached to built-in cube");
            return;
        }

        UpdateMeshUVS();
    }

    // Update is called once per frame
    void Update () 
    {
#if UNITY_EDITOR
        UpdateMeshUVS();
#endif
    }

    void UpdateMeshUVS()
    {
        Vector2[] uvs = m_mesh.uv;
        // Front
        SetFaceTexture(CubeFaceType.Front, uvs);
        // Top
        SetFaceTexture(CubeFaceType.Top, uvs);
        // Back
        SetFaceTexture(CubeFaceType.Back, uvs);
        // Bottom
        SetFaceTexture(CubeFaceType.Bottom, uvs);
        // Left
        SetFaceTexture(CubeFaceType.Left, uvs);  
        // Right        
        SetFaceTexture(CubeFaceType.Right, uvs);   
        m_mesh.uv = uvs;
    }

    Vector2[] GetUVS(float originX, float originY)
    {
        Vector2[] uvs = new Vector2[4];
        uvs[0] = new Vector2(originX / 3.0f, originY / 3.0f);
        uvs[1] = new Vector2((originX + 1) / 3.0f, originY / 3.0f);
        uvs[2] = new Vector2(originX / 3.0f, (originY + 1) / 3.0f);
        uvs[3] = new Vector2((originX + 1) / 3.0f, (originY + 1) / 3.0f);
        return uvs;
    }

    void SetFaceTexture(CubeFaceType faceType, Vector2[] uvs)
    {
        if (faceType == CubeFaceType.Front) {
            Vector2[] newUVS = GetUVS(frontPoint.x, frontPoint.y);
            uvs[0]  = newUVS[0]; 
            uvs[1]  = newUVS[1];
            uvs[2]  = newUVS[2];
            uvs[3]  = newUVS[3];
        }else if (faceType == CubeFaceType.Back) {
            Vector2[] newUVS = GetUVS(backPoint.x, backPoint.y);
            uvs[10] = newUVS[0]; 
            uvs[11] = newUVS[1]; 
            uvs[6]  = newUVS[2]; 
            uvs[7]  = newUVS[3]; 
        }else if (faceType == CubeFaceType.Top) {
            Vector2[] newUVS = GetUVS(topPoint.x, topPoint.y);
            uvs[8] = newUVS[0]; 
            uvs[9] = newUVS[1]; 
            uvs[4]  = newUVS[2]; 
            uvs[5]  = newUVS[3]; 
        }else if (faceType == CubeFaceType.Bottom) {
            Vector2[] newUVS = GetUVS(bottomPoint.x, bottomPoint.y);
            uvs[12] = newUVS[0]; 
            uvs[14] = newUVS[1]; 
            uvs[15]  = newUVS[2]; 
            uvs[13]  = newUVS[3]; 
        }else if (faceType == CubeFaceType.Left) {
            Vector2[] newUVS = GetUVS(leftPoint.x, leftPoint.y);
            uvs[16] = newUVS[0]; 
            uvs[18] = newUVS[1]; 
            uvs[19]  = newUVS[2]; 
            uvs[17]  = newUVS[3]; 
        }else if (faceType == CubeFaceType.Right) {
            Vector2[] newUVS = GetUVS(rightPoint.x, rightPoint.y);
            uvs[20] = newUVS[0]; 
            uvs[22] = newUVS[1]; 
            uvs[23]  = newUVS[2]; 
            uvs[21]  = newUVS[3]; 
        }
    }
}
  1. 使用的贴图
    分别修改Cube每个面的贴图UV(Unity3D开发之十八)

  2. 给一个Cube添加改图片材质。并添加CustomUVS.cs脚本。修改需要截取的区域原点
    分别修改Cube每个面的贴图UV(Unity3D开发之十八)

注意:由于图片是3×3的,所以截取区域中按3等分截取。

实际效果
分别修改Cube每个面的贴图UV(Unity3D开发之十八)

分类: 未分类 标签:

事件/委托机制(event/delegate)(Unity3D开发之十七)

2015年6月17日 没有评论

猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢!
原文地址: http://blog.csdn.net/cocos2der/article/details/46539433

Delegate作用我就不多说了,Unity中可以直接使用EventHandler实现事件委托,咱们直接事例吧。

一、场景物体移动结束后事件监听

假如PlayerControl,移动结束后触发MoveComplete事件。

using UnityEngine;
using System.Collections;
using System;

public class PlayerControl : MonoBehaviour {

    public event EventHandler MoveComplete;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonUp(0)) {
            // Test logic for PlayerMoveComplete
            PlayerMoveComplete();
        }
    }

    void PlayerMoveComplete()
    {
        if (MoveComplete != null) {
            MoveComplete(this, EventArgs.Empty);
        }
    }
}

事件接收处理

using UnityEngine;
using System.Collections;
using System;

public class GameManager : MonoBehaviour {
    public static GameManager Instance;
    public PlayerControl playerControl;

    void Awake () 
    {
        // check there isn't more than one instance of the GameManager in the scene
        if(Instance != null){
            Debug.LogError("More than one GameManager found in the scene");
            return;
        }
        // set the global instance
        Instance = this;
    }

    // Use this for initialization
    void Start () {
        playerControl.MoveComplete += HandleMoveComplete;
    }

    void HandleMoveComplete (object sender, EventArgs e)
    {
        Debug.Log("MoveComplete:");
    }

    // Update is called once per frame
    void Update () {

    }
}

这里由于使用的EventHandler实现,所以在事件中无法传递自定义参数。

二、自定义Event,事件中传递自定义参数

1、自定义EventArgs

using UnityEngine;
using System.Collections;
using System;

public class PlayerMoveEventArgs : EventArgs {

    private string message;

    public PlayerMoveEventArgs(string message)
    {
        this.message = message;
    }

    public string Message
    {
        get{return message;}
    }

}

public delegate void MoveCompleteHandle(object sender, PlayerMoveEventArgs e);

那么我们修改下PlayerControl

using UnityEngine;
using System.Collections;
using System;

public class PlayerControl : MonoBehaviour {

    public event EventHandler MoveComplete;

    public event MoveCompleteHandle CustomMoveComplete;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonUp(0)) {
            // Test logic for PlayerMoveComplete
            PlayerMoveComplete();
        }
    }

    void PlayerMoveComplete()
    {
        if (MoveComplete != null) {
            MoveComplete(this, EventArgs.Empty);
        }

        if (CustomMoveComplete != null) {
            CustomMoveComplete(this, new PlayerMoveEventArgs("Move:" + this.name));
        }
    }
}

处理事件的我们也修改下

using UnityEngine;
using System.Collections;
using System;

public class GameManager : MonoBehaviour {
    public static GameManager Instance;
    public PlayerControl playerControl;

    void Awake () 
    {
        // check there isn't more than one instance of the GameManager in the scene
        if(Instance != null){
            Debug.LogError("More than one GameManager found in the scene");
            return;
        }
        // set the global instance
        Instance = this;
    }

    // Use this for initialization
    void Start () {
        playerControl.MoveComplete += HandleMoveComplete;

        playerControl.CustomMoveComplete += HandleCustomMoveComplete;
    }

    void HandleCustomMoveComplete (object sender, PlayerMoveEventArgs e)
    {
        Debug.Log("HandleCustomMoveComplete:" + e.Message);
    }

    void HandleMoveComplete (object sender, EventArgs e)
    {
        Debug.Log("MoveComplete:");
    }

    // Update is called once per frame
    void Update () {

    }
}

ok,现在你可以自由的玩耍了。

分类: 未分类 标签: