ラベル C# の投稿を表示しています。 すべての投稿を表示
ラベル C# の投稿を表示しています。 すべての投稿を表示

2015年2月21日土曜日

コライダー設定[Unitty Orthello C#]

BOXコライダーの初期設定を行う
対象オブジェクトにスクリプトを追加してStartにて設定を行う。
※Vector3の値はそれぞれで修正してください

void Start () {
BoxCollider BC = this.gameObject.transform.GetComponentInChildren<BoxCollider>();
BC.center = new Vector3(0.0f , 0.0f , 200.0f);
BC.size = new Vector3(0.01f , 0.01f , 0.4f);
}

//トリガー
.//衝突開始時
void OnTriggerEnter(Collider col){

}
//衝突終了時
void OnTriggerExit(Collider col){

}

2014年7月27日日曜日

OrthelloのAnimatingSpriteでアニメーション画像を変更する [Unitty Orthello C#]

TextureとOTAnimationはそれぞれドラッグ&ドロップで配置すること

//変数宣言
int AnimationNo = 4;

public Texture PlayerTex4;
public Texture PlayerTex5;
public Texture PlayerTex7;
public OTAnimation PlayerAni4;
public OTAnimation PlayerAni5;
public OTAnimation PlayerAni7;
Texture PlayerTex;
OTAnimation PlayerAni;

//アニメーション選別
if (AnimationNo == 4) {
PlayerTex = PlayerTex4;
PlayerAni = PlayerAni4;
} else if (AnimationNo == 5) {
PlayerTex = PlayerTex5;
PlayerAni = PlayerAni5;
} else if (AnimationNo == 7) {
PlayerTex = PlayerTex7;
PlayerAni = PlayerAni7;
}

//アニメーション入れ替え
OT.AnimatingSprite("Player").spriteContainer.texture = PlayerTex;
OT.AnimatingSprite("Player").animation = PlayerAni;

2014年6月11日水曜日

Unityで文字列関連 [Unity C#]

●指定文字が文字列に含まれているか

int iNos = 文字列.IndexOf('D');

含まれている場合は位置の数値を返す。
含まれていない場合は-1を返す



●指定文字列が文字列に含まれているか
bool ans = 文字列.Contains("DDDDDD");

含まれている場合、true
含まれていない場合、false

※indexOfの場合はcharなのでシングルクォートで囲むこと、そうしないとエラーになる


●文字列中に指定文字がいくつ含まれているか
string aData = "A_B_C_";
int Cou = aData.Split(new char[] {'_'}).Length - 1;
Debug.Log ("Cou : " + Cou.ToString ());

結果:Cou : 3

プレハブでインスタンス生成時に子オブジェクト一覧を取得する [Unity C#]

子オブジェクト一覧を取得する

int iCount = transform.childCount;

bool ansTop = false;
bool ansButtom = false;

string Top = "";
string Buttom = "";

void Start () {
for(int i=0; i<iCount; i++){
Transform child = transform.GetChild (i);
Debug.Log("ObjectName : " + child.name);

if (ansTop) {
Top = child.name;
Debug.Log("Top : " + Top);
ansTop = false;
} else if (ansButtom) {
Buttom = child.name;
Debug.Log("Buttom : " + Buttom);
ansButtom = false;
}
}
}




2014年6月2日月曜日

三角関数 角度を求める [Unity C#]

球の発射系では発射位置が固定されていると思うが、その際に発射角度を求めたい場合。

float Angle;
float x;
float y;

//角度を求める
Angle = Mathf.Atan ( y / x ) * Mathf.Rad2Deg;

//対象の角度を変更
対象.transform.localRotation = Quaternion.Euler(0,0,angle);

で角度が変更出来る



2014年5月14日水曜日

子オブジェクトの取得 [Unity C#]

//オブジェクト取得
GameObject Play1;
Play1 = GameObject.Find("Character1");
Play1.transform.localPosition = new Vector3(0 , 0 , 0);

//子オブジェクト取得
GameObject Play2;
Play2 = GameObject.FindChild("Character").gameObject;
Play2.transform.localPosition = new Vector3(0 , 0 , 0);


など

2014年4月18日金曜日

親オブジェクトの取得 [Unity C#]


GameObject Parent;
Parent=  gameObject.transform.parent.gameObject;


2014年4月16日水曜日

数値の正・負の符号を反転させる [Unity C#]

■ケース1
Debug.Log ((5).ToString ());
Debug.Log (-(5).ToString ());
◆結果
5
-5


□ケース2
Debug.Log ((-5).ToString ());
Debug.Log (-(-5).ToString ());
◇結果
-5
5

2014年4月14日月曜日

Unityで一時停止処理 [Unity C#]

ゲームの最中に一時停止を行う方法(Physic)

1.Time.timeScaleを0に設定
 Time.timeScale = 0;

2.Physic以外の処理を停止させたい場合は
 各動作処理の際にTime.timeScale>0を追加する
 Time.timeScaleが0以上の場合に処理(動作させる)を行う


上記で一時停止となる。

2014年2月8日土曜日

文字列を数値変換 [Unity C#]

文字列を数値に変換する際

string str = "1234";

int i = (int)str;

とすると「 error CS0030: Cannot convert type `string' to `int'」と怒られます。

これを↓こうすると怒られません

int i = int.Parse(str);



2014年2月3日月曜日

衝突判定 [Unity C#]

void OnCollisionEnter(Collision collis) {

//衝突した相手の名前を取得
Debug.Log(collis.gameObject.name);

//衝突時の相対速度
Debug.Log(collis.relativeVelocity);

}

乱数 [Unity C#]



Random.Range (min , max);

2014年1月16日木曜日

重力加速度設定 [Unity C#]


float gX = 0.0f;
float gY = -100.0f;
float gZ = 0.0f;

Physics.gravity = new Vector3(gX , gY , gZ);


2013年12月25日水曜日

スプライトの行動範囲指定方法 [Unity C# Orthello]

スプライトの行動範囲指定方法

① 動的対象のスプライトを作成
  1.ここでは「MoveMan」で作成
  

②行動範囲用のスプライトを作成
  1.ここでは「AreaSpace」で作成

③行動範囲制御
  1.新規でC#ファイルを作成。ここでは「MoveManL」
  2.下記の通り修正する

public class MoveManL : MonoBehaviour {
OTObject otObject = null;
// Use this for initialization
void Start () {
otObject = GetComponent<OTObject>();
otObject.BoundBy(GameObject.Find("AreaSpace").GetComponent<OTObject>());
OT.Persist(otObject);
}
void OnLevelWasLoaded (int level) {
otObject.BoundBy(GameObject.Find("AreaSpace").GetComponent<OTObject>());
}
}


④C#「MoveManL」を①で作成した「MoveMan」にドラッグ&ドロップ

⑤スプライト「MoveMan」のInspector「Draggable」「RegisterInput」のチェックを入れる
「Draggable」→ドラッグ可不可
「RegisterInput」→押下可不可

※スプライト「MoveMan」がスプライト「AreaSpace」で隠れている場合は、スプライト「MoveMan」の「Depth」を調整。







2013年12月18日水曜日

クリックorタップ処理 [Unity Android C#]

1.OnMouseDown()イベント


2.Update()内に記述

if (Input.GetMouseButtonUp(0)) {
   //左クリックの場合orタップ時の処理

} else if (Input.GetMouseButtonUp(1)) {
   //右クリックの場合orダブルタップ?時の処理

} else if (Input.GetMouseButtonUp(2)) {
   //中クリックの場合or???の処理

}



2013年12月13日金曜日

実機orローカル実行時の区別 [Unity C# Android]

実機またはUnity上で実行する場合に処理を分けたい場合は下記を実装する


#if UNITY_EDITOR
//Unityで実行する場合の処理を記述
#else
//実機で実行する場合の処理を記述
#endif






2013年11月14日木曜日

Unityでビットマップフォントを使用する [Unity Orthello C#]

Orthelloのインポートとビットマップフォントの作成は終了している状態で進めます

※Orthelloのインポートはこちらから
※ビットマップフォントの作成はこちらから

1.Projectの「Assets」「Orthello」「Objects」「Sprites」「SpriteAtlas」「SpriteAtlas-Cocos2D-fnt」を「Hierarchy」にドラッグ&ドロップします


この部分が追加されます
「StatusFnt」に変更





















2. 「Assets」-「Orthello」-「Objects」-「Sprites」-「TextSprite」を「Hierarchy」にドラッグ&ドロップ













この部分が追加されます
「StatusSprite」に変更



















3.作成した「xxxx.txt」と「xxxx.png」を適当な場所に移動する
  ここでは「Assets」に「status.txt」と「status.png」を作成

4. 「StatusFnt」-「Inspector」の「Texture」を作成した「xxxx.png」に変更する





















5. 「StatusSprite」-「Inspector」の「Sprite Container」に「StatusFnt」を設定
  ※フォントカラーを変更したい場合は「TinColor」で色を選択























6.ステータス値の出力
  「Status.cs」を作成し、下記プログラムを書き込む

public class status: MonoBehaviour {
  OTTextSprite text;
  // Use this for initialization
  void Start () {
  text = GetComponent<OTTextSprite>();
  }
  int tick = 100;
  // Update is called once per frame
  void Update () {
  if (tick > 0) {
  tick -= 1;
  text.text = (tick) + "/100" ;
  }
  }
}

「Status.cs」を「StatusSprite」にドラッグ&ドロップ
実行すると「100/100」から「0/100」にカウントダウンする



Unityにデータ保存 [Unity C#]

1.保存
  EditorPrefs.SetInt("名前", 数値);

2.取得
  EditorPrefs.GetInt("名前", 0);

3.削除

  指定削除 EditorPrefs.DeleteKey("削除する名前");
  全件削除 EditorPrefs. DeleteAll();



※他にも[Bool][String][Float]があるようです
詳しくはこちらを参照。

2013年10月15日火曜日

GameObjectの表示・非表示 [Unity-C#]

1.表示する場合

gameObject.SetActive(true);



2.非表示にする場合

gameObject.SetActive(false);


※ただし、Update ()は常に実施されるので注意が必要です。





2013年10月13日日曜日

別Sceneの呼び出し方法[Unity C#]

ボタン押下時に遷移するように

void OnMouseDown(){ 
Application.LoadLevel("Scene名");
}