Skip to content

Instantly share code, notes, and snippets.

@zonaryFUND
Last active January 15, 2024 13:00
Show Gist options
  • Save zonaryFUND/4e4f9685074c08ba97830847c9eff683 to your computer and use it in GitHub Desktop.
Save zonaryFUND/4e4f9685074c08ba97830847c9eff683 to your computer and use it in GitHub Desktop.
// 受け手オブジェクトB/Dにアタッチされる何かが継承すべきinterface
public interface IDropReceptor {
bool ReceiveDrop(GameObject obj);
}
// ドロップされるオブジェクトA/Cのコンポーネントのドロップ処理
public class ComponentA: MonoBehaviour {
// 中略
public void OnEndDrag(PointerEventData eventData)
{
// 略:ドロップ先の座標に別のGameObjectがあるかRayCastやColliderなどで確認する処理
var receptor = collidingObject.GetComponent<IDropReceptor>();
if (receptor.ReceiveDrop(gameObject)) {
// A/C側の状態変更などが必要であればここで何かする
} else {
// オブジェクトをドラッグ前の位置に戻す
rectTransform.anchoredPosition = prevPos;
}
}
}
// Aだけを受け取るドロップ受け取り手オブジェクトB
public class ComponentB: MonoBehaviour, IReceiptor {
// 中略
public bool ReceiveDrop(GameObject obj) {
var componentA = obj.GetComponent<ComponentA>();
if (componentA == null) {
// ComponentAが見つからなければ、Bの上にドロップされたオブジェクトはAではないので、そのオブジェクトは元の位置に戻させる
return false;
}
// ComponentAが非nullであった場合はAがドロップされたのでここにB側ですべき処理を書く
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment