-
-
Save Sahasrara/f4148144f895ca4d32f1bee5407cc347 to your computer and use it in GitHub Desktop.
Example Scroll View Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Controller for the interact menu canvas. | |
/// </summary> | |
public class InteractMenuCanvasController : MonoBehaviour | |
{ | |
// Constants | |
private const string PanelName = "Panel"; | |
private const string ScrollViewName = "Scroll View"; | |
private const string ViewPortName = "Viewport"; | |
private const string Content = "Content"; | |
private const float FadeSpeed = 5f; | |
private const float MenuToLauncherDistance = 5f; | |
private const float PanelAlphaTarget = 0.5f; | |
// Properties | |
private GameObject canvas; | |
private GameObject panel; | |
private Image panelImage; | |
private GameObject scrollView; | |
private GameObject content; | |
private Camera mainCamera; | |
private BoxCollider2D canvasCollider; | |
private bool alive; | |
// Prefabs | |
public GameObject buttonPrefab; | |
void Awake() | |
{ | |
canvas = gameObject; | |
panel = findChildOrFail(PanelName); | |
panelImage = panel.GetComponent<Image>(); | |
Helpers.VerifyNotNull(panel, "Panel image was null!"); | |
scrollView = findChildOrFail(string.Join("/", PanelName, ScrollViewName)); | |
content = findChildOrFail(string.Join("/", PanelName, ScrollViewName, ViewPortName, Content)); | |
mainCamera = Camera.main; | |
alive = false; | |
// Set Collider Size | |
canvasCollider = gameObject.GetComponent<BoxCollider2D>(); | |
Helpers.VerifyNotNull(canvasCollider, "Canvas collider didn't exist"); | |
RectTransform canvasRect = transform.GetComponent<RectTransform>(); | |
Helpers.VerifyNotNull(canvasRect, "Canvas rect transform was null"); | |
canvasCollider.offset = new Vector2(0, 0); | |
canvasCollider.size = canvasRect.rect.size; | |
} | |
void Update() | |
{ | |
// Fade in and out | |
Fading(); | |
} | |
public void Setup(InteractMenuConfiguration config) | |
{ | |
// Position the Canvas | |
PositionCanvas(config); | |
// Setup All UI Elements | |
SetupUIElements(config); | |
// Set Fade In | |
alive = true; | |
} | |
private void PositionCanvas(InteractMenuConfiguration config) | |
{ | |
// Situate the canvas | |
Vector3 bottomLeft = mainCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)); | |
Vector3 bottomRight = mainCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)); | |
Vector3 launcherPosition = config.launcherObject.transform.position; | |
Vector3 menuPosition = transform.position; | |
// Left or Right | |
float leftDistX = Mathf.Abs(bottomLeft.x - launcherPosition.x); | |
float rightDistX = Mathf.Abs(bottomRight.x - launcherPosition.x); | |
if (leftDistX <= rightDistX) | |
{ | |
// Right | |
menuPosition.x = launcherPosition.x + MenuToLauncherDistance; | |
} | |
else if (leftDistX > rightDistX) | |
{ | |
// Left | |
menuPosition.x = launcherPosition.x - MenuToLauncherDistance; | |
} | |
// Vertical | |
menuPosition.y = launcherPosition.y; | |
// Set Position | |
transform.position = menuPosition; | |
} | |
private void SetupUIElements(InteractMenuConfiguration config) | |
{ | |
GameObject button = Instantiate(buttonPrefab); | |
RectTransform buttonTransform = (RectTransform)button.transform; | |
// Set Name | |
button.name = "Use Button"; | |
// Set Parent | |
button.transform.SetParent(content.transform); | |
// Set anchors | |
buttonTransform.anchorMin = new Vector2(0, 0); | |
buttonTransform.anchorMax = new Vector2(1, 1); | |
buttonTransform.pivot = new Vector2(0.5f, 0.5f); | |
// Position the button | |
button.transform.localScale = new Vector3(1, 1, 1); | |
button.transform.position = new Vector3(0, 0, 0); | |
Button buttonScript = button.GetComponent<Button>(); | |
GameObject buttonText = buttonScript.transform.Find("Text")?.gameObject; | |
//TextMeshProUGUI buttonTextScript = buttonText.GetComponent<TextMeshProUGUI>(); | |
//buttonTextScript.text = "Shit"; | |
} | |
public void TearDown() | |
{ | |
// Set Fade Out | |
alive = false; | |
} | |
private void Fading() | |
{ | |
if (alive) | |
{ | |
if (panelImage.color.a < PanelAlphaTarget) | |
{ | |
Color color = panelImage.color; | |
color.a += FadeSpeed * Time.deltaTime; | |
panelImage.color = color; | |
} | |
} | |
else | |
{ | |
if (panelImage.color.a > 0) | |
{ | |
Color color = panelImage.color; | |
color.a -= FadeSpeed * Time.deltaTime; | |
panelImage.color = color; | |
} | |
else | |
{ | |
Destroy(gameObject); | |
} | |
} | |
} | |
GameObject findChildOrFail(string name) | |
{ | |
Transform toFindTransform = gameObject.transform.Find(name); | |
Helpers.VerifyNotNull(toFindTransform?.gameObject, "Could not find: " + name); | |
return toFindTransform.gameObject; | |
} | |
} | |
/// <summary> | |
/// InteractMenuConfiguration contains configuration to setup the interact menu. | |
/// </summary> | |
public class InteractMenuConfiguration | |
{ | |
public GameObject launcherObject { get; } | |
public InteractMenuConfiguration(GameObject launcherObject) | |
{ | |
this.launcherObject = launcherObject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment