Last active
August 14, 2017 04:07
-
-
Save yuw-unknown/e19fc11968931694f2498e48fc859d20 to your computer and use it in GitHub Desktop.
Behaviourの共通機能 主にPosition周り
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
/** | |
* Behaviourの共通機能 | |
* FIXME: 毎回BehaviourUtilityを記載するのも冗長なので何か手を打ちたい | |
* - BUにリネーム? | |
* - 他もっと便利にアクセス出来る方法があれば | |
*/ | |
public class BehaviourUtility : MonoBehaviour { | |
void Start () { | |
} | |
void Update () { | |
} | |
// ----------------------------------------- | |
// Transform.Position | |
// ----------------------------------------- | |
/** | |
* GameObjectのTransform.positon.x,y,zを取得します | |
* GameObject.transform.positionとわざわざ書くのを防ぐためだけのものです。 | |
* | |
* - parameter gameObject: Positionを取得したいGameObject | |
* | |
* - returns: Position x,y,z | |
*/ | |
public static float PositionX(GameObject gameObject) { return gameObject.transform.position.x; } | |
public static float PositionY(GameObject gameObject) { return gameObject.transform.position.y; } | |
public static float PositionZ(GameObject gameObject) { return gameObject.transform.position.z; } | |
/** | |
* GameObjectのTransform.Positonの加算/減算を行います。 | |
* 外部から別GameObjectの座標を変更したい時に利用します。 | |
* 自分自身の座標変更の行う場合は、BaseMonoBehaviourを継承して利用してください | |
* | |
* - parameter gameObject: Position編集したいGameObject | |
* - parameter positon: 加算/減算を行いたいだけのVector | |
* | |
* - returns: 加算/減算後のPosition | |
*/ | |
public static GameObject PositionFrom(ref GameObject gameObject, Vector3 positon) { | |
Vector3 targetPositon = gameObject.transform.position; | |
targetPositon.x += positon.x; | |
targetPositon.y += positon.y; | |
targetPositon.z += positon.z; | |
gameObject.transform.position = targetPositon; | |
return gameObject; | |
} | |
/** | |
* GameObjectのTransformのx,y,z座標の加算/減算を行います。(PositionFromのラッパークラス) | |
* | |
* - parameter gameObject: Position編集したいGameObject (※参照渡し) | |
* - parameter x: 編集したいX座標 | |
* | |
* - returns: 加算/減算後のGameObject | |
*/ | |
public static GameObject PositionFromX(ref GameObject gameObject, float x) { | |
return BehaviourUtility.PositionFrom(ref gameObject, new Vector3(x, 0, 0)); | |
} | |
public static GameObject PositionFromY(ref GameObject gameObject, float y) { | |
return BehaviourUtility.PositionFrom(ref gameObject, new Vector3(0, y, 0)); | |
} | |
public static GameObject PositionFromZ(ref GameObject gameObject, float z) { | |
return BehaviourUtility.PositionFrom(ref gameObject, new Vector3(0, 0, z)); | |
} | |
/** | |
* GameObjectのTransform.positonの置き換えを行います。 | |
* コード上にGameObject.transform.positionとわざわざ書くのを防ぐためだけのものです。 | |
* | |
* - parameter gameObject: Position編集したいGameObject (※参照渡し) | |
* - parameter x: 置き換えたいVector座標 | |
* | |
* - returns: 置き換え後のGameObject | |
*/ | |
public static GameObject PositionAt(ref GameObject gameObject, Vector3 positon) { | |
gameObject.transform.position = positon; | |
return gameObject; | |
} | |
/** | |
* GameObjectのTransform.positionのx,y,z座標の置き換えを行います。(PositionAtのラッパークラス) | |
* - parameter gameObject: Position編集したいGameObject (※参照渡し) | |
* - parameter x: 置き換えたいX座標 | |
* | |
* - returns: 置き換え後のGameObject | |
*/ | |
public static GameObject PositionAtX(ref GameObject gameObject, float x) { | |
return BehaviourUtility.PositionAt( | |
ref gameObject, | |
new Vector3(x, BehaviourUtility.PositionY(gameObject), BehaviourUtility.PositionZ(gameObject)) | |
); | |
} | |
public static GameObject PositionAtY(ref GameObject gameObject, float y) { | |
return BehaviourUtility.PositionAt( | |
ref gameObject, | |
new Vector3(BehaviourUtility.PositionX(gameObject), y, BehaviourUtility.PositionZ(gameObject)) | |
); | |
} | |
public static GameObject PositionAtZ(ref GameObject gameObject, float z) { | |
return BehaviourUtility.PositionAt( | |
ref gameObject, | |
new Vector3(BehaviourUtility.PositionX(gameObject), BehaviourUtility.PositionY(gameObject), z) | |
); | |
} | |
/** | |
* GameObjectのTransform.position x,y,zの変更を行い、最大値より大きな値が入り込まないようにします | |
* | |
* - parameter gameObject: Position編集したいGameObject | |
* - parameter x,y,z: 編集したい座標 | |
* - parameter maximum: 最大値 | |
* | |
* - returns: 編集後のGameObject | |
*/ | |
public static GameObject PositionAtToMaximumX(ref GameObject gameObject, float x, float maximum) { | |
float tmpX = BehaviourUtility.PositionX(gameObject); | |
tmpX = NumberUtility.GetValueOrMaximum(tmpX + x, maximum); | |
return BehaviourUtility.PositionAtX(ref gameObject, tmpX); | |
} | |
public static GameObject PositionAtToMaximumY(ref GameObject gameObject, float y, float maximum) { | |
float tmpY = BehaviourUtility.PositionY(gameObject); | |
tmpY = NumberUtility.GetValueOrMaximum(tmpY + y, maximum); | |
return BehaviourUtility.PositionAtY(ref gameObject, tmpY); | |
} | |
public static GameObject PositionAtToMaximumZ(ref GameObject gameObject, float z, float maximum) { | |
float tmpZ = BehaviourUtility.PositionZ(gameObject); | |
tmpZ = NumberUtility.GetValueOrMaximum(tmpZ + z, maximum); | |
return BehaviourUtility.PositionAtZ(ref gameObject, tmpZ); | |
} | |
/** | |
* GameObjectのTransform.position x,y,zの変更を行い、最小値より小さな値が入り込まないようにします | |
* - parameter gameObject: Position編集したいGameObject | |
* - parameter x,y,z: 編集したい座標 | |
* - parameter minimum: 最小値 | |
* | |
* - returns: 編集後のGameObject | |
*/ | |
public static GameObject PositionAtToMinimumX(ref GameObject gameObject, float x, float minimum) { | |
float tmpX = BehaviourUtility.PositionX(gameObject); | |
tmpX = NumberUtility.GetValueOrMinimum(tmpX + x, minimum); | |
return BehaviourUtility.PositionAtX(ref gameObject, tmpX); | |
} | |
public static GameObject PositionAtToMinimumY(ref GameObject gameObject, float y, float minimum) { | |
float tmpY = BehaviourUtility.PositionY(gameObject); | |
tmpY = NumberUtility.GetValueOrMinimum(tmpY + y, minimum); | |
return BehaviourUtility.PositionAtY(ref gameObject, tmpY); | |
} | |
public static GameObject PositionAtToMinimumZ(ref GameObject gameObject, float z, float minimum) { | |
float tmpZ = BehaviourUtility.PositionZ(gameObject); | |
tmpZ = NumberUtility.GetValueOrMinimum(tmpZ + z, minimum); | |
return BehaviourUtility.PositionAtZ(ref gameObject, tmpZ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class NumberUtility { | |
/** | |
* 渡された値(value)が、境界値(limit)より上回っていたらlimitを、 | |
* そうでなければ、valueを返却します | |
* | |
* - parameter value: チェック対象値 | |
* - parameter limit: 境界値 | |
* | |
* - returns: チェック後の数値 | |
*/ | |
public static float GetValueOrMaximum(float value, float limit) { | |
float result = value; | |
if (value > limit) { | |
result = limit; | |
} | |
return result; | |
} | |
/** | |
* 渡された値(value)が、境界値(limit)より下回っていたらlimitを、 | |
* そうでなければ、valueを返却します | |
* | |
* - parameter value: チェック対象値 | |
* - parameter limit: 境界値 | |
* | |
* - returns: チェック後の数値 | |
*/ | |
public static float GetValueOrMinimum(float value, float limit) { | |
float result = value; | |
if (value < limit) { | |
result = limit; | |
} | |
return result; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment