unity做2d必备

//这个方法只适合横向的序列图片
private var startFrame:int;
function animationBase(columnSize : int , colFrameStart : int ,sizeY:float, offsetY:float , frames : int)// function for animating sprites
{																// modulate to total number of frames
	startFrame ++;
	var size:Vector2 =new Vector2 ( 1.0f / columnSize, sizeY); 
    renderer.material.mainTextureScale  = size;											
    if(startFrame % frames == 0)	
    {
        var u:float = startFrame/frames;
        if(u >= columnSize)
        {
           u = 0;
           startFrame = 0;
        }       
	    var offset = Vector2 ((u + colFrameStart) * size.x, offsetY);																// v gets current y coordinate by dividing by column size
		renderer.material.mainTextureOffset = offset;													// texture offset for diffuse map(漫反射)		
    } 

    print(startFrame + "--" +sizeY);  										// texture scale  for diffuse map										// texture scale  for bump (normal map) 
}


private var startTime : float;
function aniSprite (columnSize : int , rowSize : int, colFrameStart : int , rowFrameStart : int , totalFrames : int, framesPerSecond : int, isStart: boolean)// function for animating sprites
{
	if(isStart)
	{
		startTime = Time.time;
	}
	var index : int = (Time.time-startTime) * framesPerSecond;													// time control fps
	index = index % totalFrames;																	// modulate to total number of frames
	
	var size = Vector2 ( 1.0f / columnSize, 1.0f / rowSize);											// scale for column and row size
	
	var u = index % columnSize;																		// u gets current x coordinate from column size
	var v = index / columnSize;																		// v gets current y coordinate by dividing by column size
	var offset = Vector2 ((u + colFrameStart) * size.x,(1.0 - size.y) - (v + rowFrameStart) * size.y); // offset equals column and row
	renderer.material.mainTextureOffset = offset;													// texture offset for diffuse map(漫反射)
	renderer.material.mainTextureScale  = size;													// texture scale  for diffuse map
	if(u == totalFrames-1)
	{
		return true;
	}
	else
	{
		return false;
	}
	//renderer.material.SetTextureOffset ("_BumpMap", offset);										// texture offset for bump (normal map)(法线贴图)
	//renderer.material.SetTextureScale  ("_BumpMap", size);											// texture scale  for bump (normal map) 
}


 

你可能感兴趣的:(unity做2d必备)