Keyde
Keyde
CC#
Created by Keyde on 12/17/2023 in #help
Unity position element in global view with UI Toolkit
Hi! Has anyone tried to position a UI element (Ui Document) from the UI Toolkit in the global view? I don't want to have a HUD that this a "whole" since they are some scripts attached to each elements (spell bar, health bar, menu...) However, without using absolute position I don't feel that's possible 🤔 Is this intended to just have a global UI in only one document?
5 replies
CC#
Created by Keyde on 12/17/2023 in #help
✅ Avoid casting with inheritance chain
Hello, I'm facing a problem right now that I don't know how to handle without casting Imagine having a simple chat system with users (premium and classic) You want to display a dashboard score but only scoring users must be displayed in since there are no score for regular users. This is what I'm thinking of so far :
using System;
using System.Collections.Generic;

interface IUser
{
public string Id { get; }
}

interface IScoringUser : IUser
{
public int Score { get; }
}

class ClassicUser : IUser
{
public string Id { get; }
}

class PremiumUser : IScoringUser
{
public string Id { get; }

public int Score { get; }
}

class Chat
{
public List<IUser> Users { get; } = new();

public void JoinChat(IUser user)
{
Users.Add(user);
}
}

class ChatScoreDashboard
{
public void DisplayScore(Chat chat)
{
foreach (var user in chat.Users)
{
if (user is IScoringUser scoringUser)
{
Console.WriteLine(scoringUser.Score);
}
}
}
}
using System;
using System.Collections.Generic;

interface IUser
{
public string Id { get; }
}

interface IScoringUser : IUser
{
public int Score { get; }
}

class ClassicUser : IUser
{
public string Id { get; }
}

class PremiumUser : IScoringUser
{
public string Id { get; }

public int Score { get; }
}

class Chat
{
public List<IUser> Users { get; } = new();

public void JoinChat(IUser user)
{
Users.Add(user);
}
}

class ChatScoreDashboard
{
public void DisplayScore(Chat chat)
{
foreach (var user in chat.Users)
{
if (user is IScoringUser scoringUser)
{
Console.WriteLine(scoringUser.Score);
}
}
}
}
Can I handle this without casting? Maybe we could ask the user it's scoring, but I do not want to have any trace of scoring in regular users since they are not eligible for this feature. What about if we want to get all scoring users? Are there some edge cases where casting is just required?
8 replies