2D スプライト 俺メモ

シーンのリロード
using UnityEngine.SceneManagement;
if (Input.GetKeyDown(KeyCode.R)){
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}


徐々に向く

public static Quaternion lookat2dspeed(GameObject targetgo, GameObject mygo,float speed){
Vector3 vec = (targetgo.transform.position - mygo.transform.position).normalized;
float targetang = Mathf.Atan2(vec.y, vec.x) * Mathf.Rad2Deg;
return Quaternion.RotateTowards(mygo.transform.rotation, Quaternion.Euler(0,0, targetang), speed * Time.deltaTime);

}

unityアセット ScriptInspector3

unityのインスペクタを使ってコードを書けるアセットinspector3について書きます。

 

un

f:id:gamemaker98:20161219171807j:plain

unityでスクリプトを書くときにはvisualstudioやmonodevelop等でコードを書いていると思いますが、連携といっても別のソフトを立ち上げてやり取りするのはきついです。
とくにモニタが一つだけだと大変です。

script inspector3はこのようにunityのインスペクタで直接コードを修正できます。

コードをダブルクリクするとフローティングウインドウで開いて編集できます。

コードの補完などできます。
セーブするとコンパイラが走ります。

ちょこちょこコードを修正するのに効率アップします。
ということで僕はいつもinspector3で作業しています。

ーーーーーーーーー
タブやら分割やらも対応しています。

f:id:gamemaker98:20161219173458j:plain


更新について、2016/12現在もバージョンアップしています。
5.5で using unityengine.UIが認識しないトラブルがあったのですが、製作者さんに臨時対応を教えてもらいました。
対応方法
UIのコードを書く。エラーがでてもいいので、コード上でUIエレメントを通常通りに作ってセーブしてコンパイラする。
そうすると using Unityengin.UIが認識するようになる。
ーーーーーーーーー
using UnityEngine.UI;を書いて
private Text debugtext;こんなふうにエレメントを作る。後はセーブしてコンパイラ
debugtext = GameObject.Find("DebugText").GetComponent<Text>();

現在修正パッチを作っていてもうすぐとのことです。


2015年のアセットファイナリストにもなったアセットなので安心して使うことができます。

あれのベータのサンプル学習メモ

ベータのサンプルをコピペ用にメモ
(ベータなので仕様変更があるかもしれないのであまりかかない)

テンプレのプラットフォームサンプルを読み込んだ。
roomのタイル情報を読み込んで、オブジェクト毎のあたり判定をチェックしている方式を使っていた。

oGameオブジェクト
create
map = layer_tilemap_get_id("Collisions"); // コリジョンレイヤのタイルマップ情報を読み込む
hills = layer_get_all_elements("Hills");  Hillアセットレイアのアセット情報を読み込む。

switch(room){ ルームによりスプライトを置き換えていくコード。
 case rSand: // if we are in the sand room
  for(i=0;i<array_length_1d(hills);i++){
   if(layer_sprite_get_sprite(hills[i]) == sBGHills_grass){ 
   layer_sprite_change(hills[i],sBGHills_sand);

step
アセットを移動する

layer_sprite_x(clouds[i], layer_sprite_get_x(clouds[i])+1);
if(layer_sprite_get_x(clouds[i]) > room_width){ 画面外だったら左端に横幅分左に移動
layer_sprite_x(clouds[i],-sprite_get_width(layer_sprite_get_sprite(clouds[i])));

draw
draw_sprite(sPickupStar,0,view_wport[0]-50,50); 
draw_text(view_wport[0]-20-sprite_get_width(sPickupStar),57,score);

//-------------------------------------------------------------
oPlayer
create
カメラフォロー
view_camera[0] = camera_create_view(x-500,y-200,1000,400,0,oPlayer,-1,-1,100,100);
step(scrProcessPlayer)
足元のタイルを3点調べる。tileのtCollision
c1 = tilemap_get_at_pixel(oGame.map,x-(sprite_get_width(sprite_index)/2),y); 
c2 = tilemap_get_at_pixel(oGame.map,x+(sprite_get_width(sprite_index)/2),y); 
c3 = tilemap_get_at_pixel(oGame.map,x,y); 

y = real(y&$ffffffc0);タイルの上に移動?8進数
if( y&$3f>0 )?
----------------------------------
oGhostを参考にテストをしてみたい。

stepーscript

var xx,yy,c1,c2; // temp var only for use in this script

hspd = dir * spd; // find out how far the object has moved
x+=hspd; // apply the distance to the object

if(dir == -1){ // if moving left
image_index = 1; // set the sprite to point the correct way
c1 = -1; // reset the collision tile checkers
c2 = -1;
c1 = tilemap_get_at_pixel(oGame.map, x, y); // check the left edge of the sprite
c2 = tilemap_get_at_pixel(oGame.map,x,y+sprite_height); // check below the left edge of the sprite, since ghosts dont like heights
if*1{ // if there is something blocking the left or if there would be a fall to the left
x = (x&$ffffffc0)+sprite_width; // move the object to the edge of the tile
dir *= -1; // change the direction
}
}else if(dir == 1) // if moving right
{ // Otherwise, check collision to the right
image_index = 0; // set the sprite to point the correct way
c1 = -1 // reset the collision tile checkers
c2 = -1;
c1 = tilemap_get_at_pixel(oGame.map,x+sprite_width,y); // check the right edge of the sprite
c2 = tilemap_get_at_pixel(oGame.map,x+sprite_width,y+sprite_height); // check below the right edge of the sprite, since ghosts dont like heights
if*2{ // if there is something blocking the right or if there would be a fall to the right
x = (x&$ffffffc0); // move the object to the edge of the tile || $ffffffc0 rounds the position to the nearest 64
dir *= -1; // change the direction
}
}

 -------------------------------------

 daruinode text script

///(x,y,text,hspeed,vspeed,color,destroytime)

var textobject = instance_create_layer(argument0,argument1,"Text_Layer",obj_Text);
textobject.text = argument2;
textobject.hspeed = argument3;
textobject.vspeed = argument4;
textobject.textcolor = argument5;
textobject.alarm[0] = room_speed*argument6;

 

*1:c1 >= 1) || (c2 <= 0

*2:c1 >= 1) || (c2 <= 0

Arbor2:   自分用メモ

Arbor2のメモ。

 Arbor2はunityでステートマシンを使うことができるアセットです。



 ステートマシンはunityのMechanimにもついている。下記参照。

docs.unity3d.com

<使い分け>
アニメ素材で遷移を管理したい=>Mechanim
コード+ステートマシンで管理したい。=>Arbor

使うと便利な事。
・ステート毎に分けてコードを書けるのでシンプルでわかりやすいし、バグも見つけやすい。monodevelopを継承しているのですぐ使える。


ステートの小さな歯車をクリックしてスクリプト編集を使うと、コードが見れるので参考になる。


f:id:gamemaker98:20161113223045j:plain

開始ステートから接続されたステートに移る。
常駐ステートはいつでも条件が満たされたら実行する。
ーーーーーーーーーーーーー
挙動
 ステートの遷移はTransitionに用意されている。
f:id:gamemaker98:20161114123245j:plain
 時間や距離などで遷移する事ができる。
 即遷移はgotoTransition

-----------------------------------

実行順序

http://arbor.caitsithware.com/tutorial/coding/

Awake <=  これが最初
OnStateAwake (一回だけ実行)
OnStateBegin
Start <= 順番注意:(一回だけ実行)
Update-----
OnStateEnd
2回めのステート実行
OnStateBegin
Update------- 
OnStateEnd

----------------------------------------------------
statebehavior のコードを書くには

Project =>Create => Arbor =>StateBehavior =>C#を選択しファイルを作る。
作ったスクリプトはArborEditorで挙動追加=>scriptの中に入っている。

StateBehaviourのカスタマイズ – Arbor

スクリプトでArborを使うにはusing Arbor;を宣言する。

------------------------------------------------


変数
プロパティで設定するならseriarizeかpublicしたものを使う。

f:id:gamemaker98:20161114122421j:plain
他のステートでも同じ変数を使いたい場合はParameterContainerを使う。

f:id:gamemaker98:20161114122247j:plain
シーンを遷移する場合も同じ変数を使いたいならglobalParameterContainer。

http://caitsithware.com/assets/arbor/scriptreference/ja/class_arbor_1_1_parameter_container_internal.html

外部から利用例:基本の変数コマンドは用意されている。
--- set

gameObject.GetComponent<ParameterContainer>().SetFloat("damagex",9f);

---- get
int seeRange;

gameObject.GetComponent<ParameterContainer>().GetFloat("seeRange",out seeRange);


------------------------------------

ステートの移動はTransition 。stateLinkに接続先を指定。
public StateLink NextState;で指定したステートをエディタで繋ぐ。
Transition(NextState);

-------------------------------

イベントにメッセージを送るにはsendTrigger系。
受けるにはTriggerTransitionを常駐させる。

 

f:id:gamemaker98:20161114091241j:plain

 

外部からステートを遷移させたいとき
gameObject.GetComponent<ArborFSM>().SendTrigger("ChangeState");

----------------------------------------

一定時間ごとに条件判定させて遷移する方法は、coroutine等があるようだ。

coroutineの参考にしたサイト:

dnasoftwares.hatenablog.com

public override void OnStateBegin() {
StartCoroutine(Loop(interval));
}
private IEnumerator Loop(float interval){
while(true){
yield return new WaitForSeconds(interval);
// if (-----)  Transition(NextState);
}
public override void OnStateEnd() {
StopAllCoroutines();// ("Loop")
}

-----------------------------------------
statebehaviourから現在のステート名を見るにはstate.name;

外部から現在のステートは何か取得する場合
gameObject.GetComponent<ArborFSMInternal>().currentState.name

注意:
上記をmonoのStartから実行するとエラーが出る。
monobehaviourのStartとarborFSMのステートの実行順を理解する必要がある。

 

--------------------------------------------

 参考ツイート

 


参考にしたサイト

フォーラム:掲示板

Arbor: State Diagram Editor - Index page

-------------------------------------------------------

 

tsubakit1.hateblo.jp 

tsubakit1.hateblo.jp

 

Linq 自分メモ

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;//これが必要

public class createItemObect_Manager : MonoBehaviour {

public ItemDatabase itemdb;
public GameObject originalItemObject;
private List<Item> AllItemList = new List<Item>();
// Use this for initialization
void Start () {

foreach (Item i in itemdb.itemDB){//
AllItemList.Add(i);
//GameObject copyObj = Instantiate(originalItemObject) as GameObject;
//copyObj.GetComponent<ItemObject>().item = i;
}

var itemList = AllItemList.Where(n => n.itemType ==Item.ItemType.Potion).ToList();

var weaponlist = AllItemList.Where(n => n.itemType ==Item.ItemType.Weapon).Select(n => n.itemAttack*2).ToList();

foreach(var n in weaponlist){
Debug.Log("attack:"+n);
}


}
}

unity fungus シーンの移動

サブルーチンのような使い方

1:呼び出し元にFlow-->Callを追加
2:呼び出すブロックを指定する。(必要に応じてflowchartやlabelを設定する)
3:Callmodeに wait until finishedを指定する。

呼び出しブロックにStopBlockを設定しとくと、そこでそのブロック処理が止まり呼び出し元に戻る。

f:id:gamemaker98:20170501091844p:plain

f:id:gamemaker98:20170501091855p:plain


同様にcallmodeでcontinueを使うと変数計算等のマクロのような使い方ができそう?(要検証)
callmodeにstopだとそのまま終了するようだ。(Jumpとの違いがわからん)

 

 

 

 

//--fungusにload等のcommandがあったので不要。

using UnityEngine;
using System.Collections;
using Fungus;
using UnityEngine.SceneManagement;
using UnityEditor;
[CommandInfo("Command","reload scene","reload scene")]
public class LoadScene_funguscommand : Command {

// Use this for initialization
public override void OnEnter(){
// do
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
// Continue();

}
}

[CommandInfo("Command","load nextScene","next scene")]
public class nextScene_funguscommand : Command {

// Use this for initialization
public override void OnEnter(){
// do
SceneManager.LoadScene((SceneManager.GetActiveScene().buildIndex+1), LoadSceneMode.Single);
Continue();

}
}

[CommandInfo("Command","goto scenename","goto scene")]
public class gotoScene_funguscommand : Command {

public string sceneName;
// Use this for initialization
public override void OnEnter(){
// do
SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
//SceneManager.LoadScene(sceneName);
// Continue();

}
}

 

//--------

 

 

unity C# 継承とgetcomponentを調べた

unity を勉強中な素人なので記事に保証はありません。

unityでC#を勉強した覚書きを書こうかと思います。

 

monobehaviourを継承したオブジェクトはnewではなくgetcomponentを使用するようにメッセージがでたのでgetcomponentをテストした。

-------------------------using 省略

testA.C# monobehaviourを継承している

public class testA : MonoBehaviour {

 public void attack()
 {
      Debug.Log("attack A");
 }
}

Aを継承したBを作る

testB.C#

public class testB : testA {

 public void attack()
 {
        Debug.Log("attackB");
 }
}

●空のgameObjectのTestBを作成しtestBスクリプトをくっつける

testC.C#

 

public GameObject objB;
void Start () {
 testB testb = objB.GetComponent<testB>();
 testb.attack();
 testA testab = objB.GetComponent<testB>();
 testab.attack();
 testA testaa = objB.GetComponent<testA>();
 testaa.attack();
}
}

●空のgameObjectのTestCを作成してtestCスクリプトをくっつける。

objBにTestBおぶじぇくとを指定する。

 

実行結果

attackB
attackA
attackA

 

ちなみにtestB testba = objB.GetComponent<testA>();
testba.attack();  型の問題でエラーになる
testB testb = objB.GetComponent<testA>() as testB;とすると通るが意味がない

 

------------------------
全ての結果をattackBと表示させるには?overrideとvartualを使う

 

testAを書き直す
public virtual void attack()
{
   Debug.Log("attack A");
}

testBを書き直す
public override void attack()
{
   Debug.Log("attackB");
}

結果は全てattackBと表示する。