[工具]Unity3D 常用方法封装

public static GameObjectGet2DTouchObject()

{

GameObjecttouchObject=null;

RaycastHit2Dhit=Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Utility.GetPosition()),Vector2.zero);

if(hit.transform!=null)

touchObject=hit.transform.gameObject;

returntouchObject;

}

publicstaticVector3GetTouchPosition(floatdepth)

{

Vector3touchPosition=Vector3.zero;

#ifUNITY_EDITOR||UNITY_STANDALONE_WIN||UNITY_STANDALONE_OSX||UNITY_METRO

touchPosition=Input.mousePosition;

#else

if(Input.touchCount>0){

touchPosition = Input.GetTouch(0).position;

}

#endif

touchPosition.z=depth;

returnCamera.main.ScreenToWorldPoint(touchPosition);

}

publicstaticVector3GetPosition()

{

Vector3touchPosition=Vector3.zero;

#ifUNITY_EDITOR||UNITY_STANDALONE_WIN||UNITY_STANDALONE_OSX||UNITY_METRO

touchPosition=Input.mousePosition;

#else

if(Input.touchCount>0){

touchPosition = Input.GetTouch(0).position;

}

#endif

returntouchPosition;

}

publicstaticRaycastHitIsHit(Vector3position,Vector3direction,floatdistance)

{

RaycastHithit;

Debug.DrawRay(position,direction*distance,Color.red);

if(Physics.Raycast(position,direction,outhit,distance)){

returnhit;

}

returnhit;

}

publicstaticRaycastHitIsHit(Vector3position,Vector3direction,floatdistance,LayerMasklayerMask)

{

Rayray=Camera.main.ScreenPointToRay(position);

RaycastHithit;

Debug.DrawRay(ray.origin,ray.direction*distance,Color.red);

if(Physics.Raycast(ray.origin,ray.direction,outhit,distance,layerMask)){

returnhit;

}

returnhit;

}

publicstaticRaycastHitGetHit(Vector3screenPosition,floatdistance,LayerMasklayerMask)

{

Rayray=Camera.main.ScreenPointToRay(screenPosition);

RaycastHithit;

Debug.DrawRay(ray.origin,ray.direction*distance,Color.red);

if(Physics.Raycast(ray.origin,ray.direction,outhit,distance,layerMask)){

returnhit;

}

returnhit;

}

publicstaticRaycastHit2DGetHit2D(Vector3screenPosition,floatdistance,LayerMasklayerMask)

{

Rayray=Camera.main.ScreenPointToRay(screenPosition);

Ray2Dray2D=newRay2D((Vector2)ray.origin,(Vector2)ray.direction);

Debug.DrawRay(ray2D.origin,Vector3.forward*distance,Color.red);

RaycastHit2Dhit=Physics2D.Raycast(ray2D.origin,ray2D.direction,distance,layerMask,0.2f,500f);

if(hit.collider!=null){

returnhit;

}

returnhit;

}

publicstaticCollider[]GetOverlapSphere(Vector3position,floatradius)

{

returnPhysics.OverlapSphere(position,radius);

}

publicstaticCollider[]GetOverlapSphere(Vector3position,floatradius,LayerMasklayerMask)

{

returnPhysics.OverlapSphere(position,radius,layerMask);

}

publicstaticRaycastHitGetLineCast(Vector3start,Vector3end)

{

RaycastHithit;

Debug.DrawLine(start,end,Color.red);

if(Physics.Linecast(start,end,outhit))

returnhit;

returnhit;

}

publicstaticRaycastHitGetLineCast(Vector3start,Vector3end,LayerMasklayerMask)

{

RaycastHithit;

Debug.DrawLine(start,end,Color.red);

if(Physics.Linecast(start,end,outhit,layerMask))

returnhit;

returnhit;

}

publicstaticboolGetTouchState()

{

#ifUNITY_EDITOR||UNITY_STANDALONE_WIN||UNITY_STANDALONE_OSX||UNITY_METRO

if(Input.GetMouseButtonDown(0))

returntrue;

if(Input.GetMouseButton(0))

returntrue;

elseif(Input.GetMouseButtonUp(0))

returnfalse;

#else

if(Input.touchCount>0){

Touch touch = Input.GetTouch(0);

if(touch.phase == TouchPhase.Began)

return true;

else if(touch.phase == TouchPhase.Moved)

return true;

else if(touch.phase == TouchPhase.Stationary)

return true;

else if(touch.phase == TouchPhase.Canceled)

return false;

else if(touch.phase == TouchPhase.Ended)

return false;

}

#endif

returnfalse;

}

publicstaticVector3GetDirection(Vector3firstPoint,Vector3secondPoint)

{

Vector3theRetDirection=Vector3.zero;

theRetDirection=firstPoint-secondPoint;

theRetDirection.z=0;

theRetDirection.Normalize();

returntheRetDirection;

}

publicstaticfloatGetAngle(Vector3moveDirection)

{

floattargetAngle=Mathf.Atan2(moveDirection.y,moveDirection.x)*Mathf.Rad2Deg-90;

returntargetAngle;

}

publicstaticfloatGetAngleBetweenTwoPoint(Vector3firstPoint,Vector3secondPoint)

{

Vector3theRetDirection=Vector3.zero;

theRetDirection=firstPoint-secondPoint;

theRetDirection.z=0;

theRetDirection.Normalize();

floattargetAngle=GetAngle(theRetDirection);

returntargetAngle;

}

publicstaticboolisOverlapCircle(Vector3pos,floatradius)

{

boolisIn=true;

Collider2D[]colliders=Physics2D.OverlapCircleAll(pos,radius);

if(colliders.Length<=0)

isIn=false;

returnisIn;

}

publicstaticboolisOverlapAreaAll(Vector3pointA,Vector3pointB)

{

boolisIn=true;

Collider2D[]colliders=Physics2D.OverlapAreaAll(pointA,pointB);

if(colliders.Length<=0)

isIn=false;

returnisIn;

}

你可能感兴趣的:([工具]Unity3D 常用方法封装)