SpriteStudio / SS5PlayerForUnity

OPTPiX SpriteStudio 5 Player for Unity
http://www.webtech.co.jp/spritestudio/
MIT License
39 stars 15 forks source link

スクリプト上からのアニメーション切り替えに関しまして #96

Closed kanata3 closed 9 years ago

kanata3 commented 9 years ago

お世話になります。

下記の様なコードを *_Control に付与し方向キーの入力でアニメーションの切り替えを行おうとしております。 (長い為、一部コードを削除して掲載しています)

enum PlayerAnimation { Back=0, Default=1, Front = 2, Left = 3, Right = 4 }

public class Player : MonoBehaviour {

private Script_SpriteStudio_PartsRoot rootAnimation;
private PlayerAnimation animationNow    = PlayerAnimation.Front;

// Use this for initialization
void Start () {
    Script_SpriteStudio_LinkPrefab link = this.GetComponent<Script_SpriteStudio_LinkPrefab>();

    Debug.Log(((GameObject)link.LinkPrefab).name);

    rootAnimation = ((GameObject)link.LinkPrefab).GetComponent<Script_SpriteStudio_PartsRoot>();

    rootAnimation.AnimationStop();
    rootAnimation.AnimationPlay((int)PlayerAnimation.Default, 1, -1, 1.0f, (Script_SpriteStudio_PartsRoot.PlayStyle.NORMAL), "", int.MaxValue, "", int.MaxValue);
}

// Update is called once per frame
void Update () {
    // 右・左
    float x = Input.GetAxisRaw("Horizontal");

    // 上・下
    float y = Input.GetAxisRaw("Vertical");

    // 移動する向きを求める
    Vector2 direction = new Vector2(x, y).normalized;

    // アニメ変更
    Animation(x, y);
}

// 方向によりアニメ変更
void Animation(float x, float y)
{
    PlayerAnimation next_animation;

    float x_abs = Mathf.Abs(x);
    float y_abs = Mathf.Abs(y);

    if( x_abs==0 && y_abs==0 )
    {
        next_animation = PlayerAnimation.Default;
    }
    else
    {
        if( x_abs==y_abs )
        {
            if (x > 0)
            {
                next_animation = PlayerAnimation.Right;
            }
            else
            {
                next_animation = PlayerAnimation.Left;
            }
        }
        else if( x_abs>y_abs )
        {
            if( x > 0 )
            {
                next_animation  = PlayerAnimation.Right;
            }
            else
            {
                next_animation = PlayerAnimation.Left;
            }
        }
        else
        {
            if (y > 0)
            {
                next_animation = PlayerAnimation.Back;
            }
            else
            {
                next_animation = PlayerAnimation.Front;
            }
        }
    }

    if( animationNow!= next_animation)
    {
        //Debug.Log(next_animation);
        animationNow = next_animation;

        Debug.Log("before: " + rootAnimation.AnimationNo);

        rootAnimation.AnimationStop();
        rootAnimation.AnimationPlay((int)animationNow, 1, -1, 1.0f, (Script_SpriteStudio_PartsRoot.PlayStyle.NORMAL), "", int.MaxValue, "", int.MaxValue);

        Debug.Log("after: " + rootAnimation.AnimationNo);
    }
}

}


Start()時の下記コードで *_Control の元になっている prefab のオブジェクトは参照できていることを確認しています。

Debug.Log(((GameObject)link.LinkPrefab).name);

また、Update()時の下記コードでは Before と After で AnimationNo が変わっている事を確認しています。

Debug.Log("before: " + rootAnimation.AnimationNo);

rootAnimation.AnimationStop();
rootAnimation.AnimationPlay((int)animationNow, 1, -1, 1.0f, (Script_SpriteStudio_PartsRoot.PlayStyle.NORMAL), "", int.MaxValue, "", int.MaxValue);

Debug.Log("after: " + rootAnimation.AnimationNo);

ですが、表示側でアニメーションが切り替わってくれません。

その代わり、プレビューを終えると最後に AnimationPlay() で切り替えたアニメに *_Control の元となっている Prefab の Animation Name の値がUnityのUI上で替わっています。

こちら本来はどのように変更すべき物でしょうか?

kanata3 commented 9 years ago

すみません、エディタの右側にタグ付けの領域が表示されないため、 question タグをつけられませんでした

MasamiYitsuse commented 9 years ago

レスポンスが遅れ申し訳ありません。 頂いた症状を再現確認と、対処方法(バグであればSS5Playerのバージョンアップないし、暫定パッチ対処)を連絡させて頂きます。 大変申し訳ございませんが、少しお時間をいただけますと助かります。

kanata3 commented 9 years ago

ありがとうございます。

ただ、本日 Facebook の方の助け合い所から github 以外に Asset Store からも SS5PlayerForUnity をダウンロード出来る様になってると聞きまして入れた所、一緒に入ったサンプルコードを見て原因がわかりました。

私の方では下記の様に Script_SpriteStudio_PartsRoot を取得していたのですが

Script_SpriteStudio_LinkPrefab link = this.GetComponent(); Script_SpriteStudio_PartsRoot rootAnimation = ((GameObject)link.LinkPrefab).GetComponent();

ソースをサンプルのように下記で取得することで解決しました。

Script_SpriteStudio_PartsRoot rootAnimation = GetComponentInChildren();

私の方の取得方法だと細かい原因はわかりませんが project に有る側のオブジェクトの値が変わるだけでヒエラルキー上に生成されたオブジェクトの値は変わらないようです。

たぶん、こちらのコードの問題なのでご対処は頂かなくて大丈夫です。

ご対応いただき有難うございました。

MasamiYitsuse commented 9 years ago

お世話になっております。 こちらからの対応の返信が遅れ、お手数をおかけしてしまい、大変申し訳ございません。

方法の差異を見ての所見ですが……恐らくLinkPrefabで格納されているGameObjectはプレハブへの参照になっているため、実際にInstantiateされている(GetComponentInChildrenで取得できる)方の参照を取得しないと、実体への反映がされなかったのではないかと思われます。

重ねて、お手数をおかけして申し訳ございません。