通过代码创建2d贴图
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public abstract class CustomMesh : MonoBehaviour
{
protected bool _isMaterialInitialized;
protected Vector2 _bounds;
protected MeshFilter _meshFilter;
protected MeshRenderer _meshRenderer;
protected Color _color;
protected Texture _texture;
protected Shader _shader;
public Vector2 Bounds
{
get { return _bounds; }
}
public bool IsVisible
{
get
{
return _meshRenderer != null ? _meshRenderer.enabled : false;
}
set
{
if (_meshRenderer != null)
{
_meshRenderer.enabled = value;
}
}
}
public virtual Color Color
{
get
{
return _color;
}
set
{
_color = value;
if (_meshRenderer != null)
{
_meshRenderer.material.SetColor("_Color", value);
}
}
}
public float Alpha
{
get
{
return _color.a;
}
set
{
_color.a = value;
if (_meshRenderer != null)
{
_meshRenderer.material.SetColor("_Color", _color);
}
}
}
public Texture Texture
{
get
{
return _texture;
}
set
{
_texture = value;
if (_meshRenderer != null)
{
_meshRenderer.material.SetTexture("_MainTex", value);
}
}
}
protected virtual void Reset()
{
_meshRenderer = GetComponent<MeshRenderer>();
if (_meshRenderer == null)
{
_meshRenderer = gameObject.AddComponent<MeshRenderer>();
}
_meshFilter = GetComponent<MeshFilter>();
if (_meshFilter == null)
{
_meshFilter = gameObject.AddComponent<MeshFilter>();
}
Mesh mesh = GenerateMesh();
if (_meshFilter.sharedMesh != null)
{
if (Application.isPlaying)
{
Destroy(_meshFilter.sharedMesh);
}
else
{
DestroyImmediate(_meshFilter.sharedMesh);
}
}
_meshFilter.mesh = mesh;
if (_isMaterialInitialized == false)
{
_shader = Shader.Find("Unlit/Transparent");
_meshRenderer.sharedMaterial = new Material(_shader);
_meshRenderer.sharedMaterial.SetTexture("_MainTex", _texture);
_isMaterialInitialized = true;
}
}
protected abstract Mesh GenerateMesh();
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class QuadMesh : CustomMesh
{
[SerializeField]
private Camera _camera;
public void MakePixelPerfect()
{
if (_texture == null)
{
_texture = gameObject.GetComponent<MeshRenderer>().sharedMaterial.GetTexture("_MainTex");
}
transform.localScale = TransformSizeFromScreenToWorld(
new Vector2(_texture.width, _texture.height));
}
public void MakeHighAsScreen()
{
MakePixelPerfect();
float factor = transform.localScale.y / (_camera.orthographicSize * 2);
transform.localScale /= factor;
}
public Vector3 TransformSizeFromScreenToWorld(Vector2 screenSize)
{
float cameraSizeY = _camera.orthographicSize;
float cameraSizeX = cameraSizeY / Screen.height * Screen.width;
Vector3 worldSize = Vector3.zero;
worldSize.x = screenSize.x / Screen.width * cameraSizeX * 2;
worldSize.y = screenSize.y / Screen.height * cameraSizeY * 2;
return worldSize;
}
#region Quad Parameters
private Vector3[] quadVerts =
{
new Vector3(-0.5f, -0.5f),
补充:综合编程 , 其他综合 ,