Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9364

【Unity】DOTweenでたまに見るあの警告への対処方法

$
0
0

はじめに

Unityでトゥイーンと言えば、アセットDOTweenが有名です。
Free版でも十分な機能を備えている上に大変使いやすく、個人的にはもはや外せないくらいです。

さて、DOTweenを使って開発しているとたまに出くわす警告(もしくはエラー)があります。
毎回対処するのですが、しばらくするとやり方を忘れてしまうのでメモがてら共有します。
ちなみに警告の状態であれば多分放置してても特に実害はないです。なんか気持ち悪いので消したいという人へ。

警告(エラー)内容

警告の場合

エディタ再生を終了したタイミングで下記の警告が出ます。

dotween_warning.png

DOTWEEN ▶  SAFE MODE ▶ DOTween's safe mode captured [エラー数] errors. This is usually ok (it's what safe mode is there for) but if your game is encountering issues you should set Log Behaviour to Default in DOTween Utility Panel in order to get detailed warnings when an error is captured (consider that these errors are always on the user side).

エラーの場合

Utility Panelで「Safe Mode」のチェックを外していると、エディタ再生中に警告ではなくエラーが出ます。

dotween_error.png

MissingReferenceException: The object of type '[なんかのコンポーネント]' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.

dotween_utility.png

発生する状況

例えば、こんな感じのスクリプトがあったとします。
RectTransformをちょっと動かすだけの単純なコードです。

DotweenTest.cs
usingDG.Tweening;usingUnityEngine;[RequireComponent(typeof(RectTransform))]publicclassDotweenTest:MonoBehaviour{privateRectTransform_rectTransform=null;privatevoidAwake(){_rectTransform=GetComponent<RectTransform>();}privatevoidStart(){// Tween開始Vector2start=Vector2.zero;Vector2end=newVector2(0f,-30f);_rectTransform.anchoredPosition=start;_rectTransform.DOAnchorPos(end,10f).SetEase(Ease.OutQuart);}}

これを動かし、トゥイーンが終わる前に(シーン遷移などで)アタッチされているゲームオブジェクトが破棄されると上の警告(エラー)が出るようになります。
ゲームオブジェクトはもうないのに、それに対するトゥイーン処理が残り続けているのがマズいみたいですね。

対処方法

(これがトゥイーン完了まで消えない前提で組まれているとかならそのままでもいいかもしれませんが、)一番はゲームオブジェクトが消える際にトゥイーンも一緒に破棄してあげることかなと思います。
具体的にはTweenerを覚えておいてOnDisableの中でKillです。
(こいつは何を言っているんだ???…って感じかと思いますが、下記のコードを見て頂ければ一発かと思います。)

DotweenTest.cs
usingDG.Tweening;usingUnityEngine;[RequireComponent(typeof(RectTransform))]publicclassDotweenTest:MonoBehaviour{privateRectTransform_rectTransform=null;privateTweener_tweener=null;privatevoidAwake(){_rectTransform=GetComponent<RectTransform>();}privatevoidStart(){// Tween開始Vector2start=Vector2.zero;Vector2end=newVector2(0f,-30f);_rectTransform.anchoredPosition=start;_tweener=_rectTransform.DOAnchorPos(end,10f).SetEase(Ease.OutQuart);}privatevoidOnDisable(){// Tween破棄if(DOTween.instance!=null){_tweener?.Kill();}}}

補足

上のコードのif判定ですが、エディタ再生終了時のオブジェクト破棄順序次第でDOTweenComponent(画像参照)が先に破棄されると出る後述のエラーを回避するためです。
シングルトンあるある。

dotween_component.png

エラーメッセージ
Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)

参考


Viewing all articles
Browse latest Browse all 9364

Latest Images

Trending Articles