projectEli/Assets/Inventory System/Scripts/UI/MoveChildrenUI.cs

71 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveChildrenUI : MonoBehaviour
{
public Transform source;
public Transform target;
// Start is called before the first frame update
void Start()
{
if(source == null || target == null)
{
Debug.LogError("MoveChildrenUI: Source and Target must not be null");
}
}
public void MoveToSource(bool sourceIsActive, bool targetIsActive)
{
Move(target, source);
target.gameObject.SetActive(targetIsActive);
source.gameObject.SetActive(sourceIsActive);
}
public void MoveToTarget(bool sourceIsActive, bool targetIsActive)
{
Move(source, target);
source.gameObject.SetActive(sourceIsActive);
target.gameObject.SetActive(targetIsActive);
}
public void ToggleMove(bool sourceIsActive, bool targetIsActive)
{
if (source.childCount == 0)
MoveToSource(sourceIsActive, targetIsActive);
else
MoveToTarget(sourceIsActive, targetIsActive);
}
private void Move(Transform s, Transform t)
{
if (s.gameObject.activeInHierarchy)
{
Transform[] children = new Transform[s.childCount];
int i = 0;
foreach(Transform child in s)
{
children[i] = child;
i++;
}
foreach (Transform child in children)
{
if(child.gameObject.activeSelf)
child.SetParent(t, false);
}
}
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
}
}