下記に載っている2Dのパラパラアニメーションを使って、Unityのキャラクターの2Dアニメーションを作ろうと思っていたのですが、移動するために矢印キーを押した途端、押すまで表示されていた画像と当たり判定も消えるというバグが起こりました。
今まで調べていましたがレイヤーも問題なかったり、画像と背景の座標も全て問題が無かった他、スクリプトをプロジェクトから消しても状況が変わらなかったので正直今の自分に打つ手がありません。
助けて下さい、お願いします。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditorInternal;
#endif
public class PlayerAnimationScript : MonoBehaviour
{
//アニメーション [SerializeField] Image spriteImage; [SerializeField, HideInInspector] Sprite[] spriteTextures; [SerializeField] public float animationFrameSeconds; [SerializeField] public bool debug; public bool loop; Coroutine runCoroutine; // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.T) && debug) { SpriteAnimeStart(); } } public void SpriteAnimeStart() { if (runCoroutine == null) { runCoroutine = StartCoroutine(SpriteAnimeCoroutine()); } } IEnumerator SpriteAnimeCoroutine() { if(loop) { while(loop) { for (int i = 0; i < spriteTextures.Length; i++) { spriteImage.sprite = spriteTextures[i]; yield return new WaitForSeconds(animationFrameSeconds); } } runCoroutine = null; yield break; } else { for (int i = 0; i < spriteTextures.Length; i++) { spriteImage.sprite = spriteTextures[i]; yield return new WaitForSeconds(animationFrameSeconds); } runCoroutine = null; } }
#if UNITY_EDITOR
[CustomEditor(typeof(PlayerAnimationScript))] public class SpriteAnimationEditor:Editor { ReorderableList reorderableList; void OnEnable() { SerializedProperty prop = serializedObject.FindProperty("spriteTextures"); reorderableList = new ReorderableList(serializedObject, prop); reorderableList.drawHeaderCallback = (rect) => EditorGUI.LabelField(rect, "Player"); reorderableList.drawElementCallback = (rect, index, isActive,isFocused) => { SerializedProperty element = prop.GetArrayElementAtIndex(index); rect.height -= 4; rect.y += 2; EditorGUI.PropertyField(rect, element, new GUIContent("フレーム" + index)); }; } public override void OnInspectorGUI() { base.OnInspectorGUI(); serializedObject.Update(); reorderableList.DoLayoutList(); serializedObject.ApplyModifiedProperties(); } }
#endif
}
ちょっと見辛いのでマークダウン使ってもらってもいいですか。
あと[SerializeField]で定義されてる変数は初期値に何がはいってるんでしょうか?
無知すみません。マークダウン使ってとは具体的に何をすれば…。
一度調べてみたら箇条書きには「-」か「*」しろとかあったのですが、それでしょうか?
[SerializeField]で定義されている変数に初期値は無かったです。
```
この3点リードみたいなのでコードを包むことで、コードが見やすくなります。コードを貼る場合はこれを使ってください。
```
こうでしょうか?
、、、
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
if UNITY_EDITOR
using UnityEditor;
using UnityEditorInternal;
endif
public class PlayerAnimationScript : MonoBehaviour
{
//アニメーション
[SerializeField]
Image spriteImage;
[SerializeField, HideInInspector]
Sprite[] spriteTextures;
[SerializeField]
public float animationFrameSeconds;
[SerializeField]
public bool debug;
public bool loop;
Coroutine runCoroutine;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.T) && debug)
{
SpriteAnimeStart();
}
}
public void SpriteAnimeStart()
{
if (runCoroutine == null)
{
runCoroutine = StartCoroutine(SpriteAnimeCoroutine());
}
}
IEnumerator SpriteAnimeCoroutine()
{
if(loop)
{
while(loop)
{
for (int i = 0; i < spriteTextures.Length; i++)
{
spriteImage.sprite = spriteTextures[i];
yield return new WaitForSeconds(animationFrameSeconds);
}
}
runCoroutine = null;
yield break;
}
else
{
for (int i = 0; i < spriteTextures.Length; i++)
{
spriteImage.sprite = spriteTextures[i];
yield return new WaitForSeconds(animationFrameSeconds);
}
runCoroutine = null;
}
}
if UNITY_EDITOR
[CustomEditor(typeof(PlayerAnimationScript))]
public class SpriteAnimationEditor:Editor
{
ReorderableList reorderableList;
void OnEnable()
{
SerializedProperty prop = serializedObject.FindProperty("spriteTextures");
reorderableList = new ReorderableList(serializedObject, prop);
reorderableList.drawHeaderCallback = (rect) => EditorGUI.LabelField(rect, "Player");
reorderableList.drawElementCallback = (rect, index, isActive,isFocused) =>
{
SerializedProperty element = prop.GetArrayElementAtIndex(index);
rect.height -= 4;
rect.y += 2;
EditorGUI.PropertyField(rect, element, new GUIContent("フレーム" + index));
};
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
serializedObject.Update();
reorderableList.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
}
endif
}
、、、
そうです。ただそれはおそらく全角なので半角にしてください。
それとコメント欄でマークダウン方式は無効なので、質問を編集するか次回以降の参考にでもしていただければ大丈夫です
回答1件
あなたの回答
tips
プレビュー