Changing the uss style sheet of a Visual Element during runtime.

I want to be able to use a default uxml as inventory for example. I need the structure of the inventory to be the same, but the uss changes per game or even per scene.

I used a game object that I called „Scene Configuration“. (could make it a scriptable object and integrate into game manager, but that is for the future).

The Scene Configuration has an entry

public StyleSheet inventoryUSSFile;

There I drag the custom uss file for the generic inventory uxml.

In the controller for the Inventory UI I read this value from the scene configuraton. This is the uss file asset I want to apply to the inventory on startup.

        private void SetSceneDependantInventoryUSS()
        {
            // get the style sheet list

            VisualElementStyleSheetSet styleSheets = m_Root.styleSheets;

            if (sceneConfiguration.GetSceneInventoryUSS() != null)
            {
                m_Root.styleSheets.Clear();

                m_Root.styleSheets.Add(sceneConfiguration.GetSceneInventoryUSS());
            }
        }

I get the list of style sheets attached to the uxml, but rather a VisualElementStyleSheetSet, that has the ability to add uss style sheet assets to the Visual Element. Thus you need to work directly with the styleSheets object from the Visual Element.

Then I clear all active stylesheets and add mine. Done. No refresh needed.

Unity3D : Global Debug On/Off

I have a custom template class for new C# class in Unity. Mostly cause I was too „lazy“ to use a debug singelton class or enhance the GameObject/Object class with a custom debug function.

        protected void dbg(string message, bool error = false)
        {
            if (debugMode & !error)
                Debug.Log("[ " + this.GetType().Name + " ( " + Time.time + " / " + Time.frameCount + " )] " + message);

            if (error)
                Debug.LogError("<color=\"red\">[" + this.GetType().Name + " (" + Time.time + ")] " + message + "</color>");
        }

So now I have a lot of Classes using this function and it can be turned off in the inspector with a toggle. But after a while of course this ends in chaos, since when you just want a certain aspect you are working appear in the console, you have to go through all of those classes and turn debug off manualy.

Instead I now added a static menu function that uses reflection to turn the debug state of each class on or off globally.

#if UNITY_EDITOR

using UnityEditor;
using System.Reflection;

#endif

        [MenuItem("InterAction Project/Global Debug Toogle - On")]
        public static void GlobalDebugToggleOn()
        {
            List<Component> componentsWithDebugModeMember = new List<Component>();

            GameObject[] allGameObjects = FindObjectsOfType<GameObject>();

            foreach (GameObject gO in allGameObjects)
            {
                Component[] components = gO.GetComponents(typeof(Component));
                foreach (Component component in components)
                {
                    Type typeOfComponent;

                    typeOfComponent = component.GetType();

                    var f = typeOfComponent.GetField("debugMode");

                    if (f != null)
                    {
                        foreach (FieldInfo propBase in component.GetType().GetFields())
                        {
     
                            if (propBase.Name == "debugMode")
                                propBase.SetValue(component, true);

                        }

                    }
                }
            }
        }

Then I can turn on only the Classes I actually need to display debug informations at the moment. Nothing fancy, but I think it may be useful for some of you out there. 🙂

I need an inventory for Interaction Project – Part 2

Actually, the Game Foundation package from Unity itself is pretty neat.

I just need the Inventory System from it for now. It also offers a currency system, a trade system with a transaction system and some kind of reward system.

There are some tutorials on the package page, yet they are not updated to the latest version and some things have changed, so one must think a bit.

It is pretty easy to use and the editor interfaces are very nice.

Example of an InventoryItem definition, on the left side the overview with all inventory item definition, well there is just this one at the moment, so 🙂

Since I want to be able to choose an item definition in an Action, so that the item can be instanced and given to the player, I need a way to list all item definitions in the catalog.

This is done by using one of the catalog API methods, as shown by a post on the unity forums. At the start I was not sure which type to use for the CatalogItemAsset ICollection which is of course an InventoryItemDefinition List.

    // You can use any type of catalog item for this list.
    List<InventoryItemDefinition> itemDefinitions = new List<InventoryItemDefinition>();
    GameFoundationSdk.catalog.GetItems(itemDefinitions);
 
    foreach (InventoryItemDefinition definition in itemDefinitions)
    {
        // Process your definition here ...
    }

My next problem is that I want a nice way to present the available ItemDefinitions in the custom Action Editor. Right now one could go into the catalag asset and open it and drag the containing InventoryItemDefinition Scritable into the Action ItemToAdd List, but yikes.

So what I really, really would like to have is that nice overview and filter/search listing element in the image above and let it pop up in a window so one can just doubleclick on an InventoryItemDefition and it would add it to the list in the custom Action inspector. Hmm. Onwards to adventure, maybe I can check out how to do this.

The Generic Menu – bon appetité – Part 1

(this article is more for myself and for future documentation, but it might be interesting for people struggling with Generic Menus.)

For my Interaction Project one main goal is that you can add Flags easily to an Actionable Object Flag Dependency.

Meaning : If there is a chest which can be opened, then you add a flag to it like „open“. The open and the close action then depend and act on the state of that flag.

For adding a flag dependency to an Actionable Object, I wanted a fast, organized and quick way to choose a flag. For this I use an Generic Menu in the Editor Script for the Flag Dependency Class, a custom Property Drawer.

(Of course, if you have multiple chest this gets a bit confusing when they have the same name, but I will add a menu entry for the flags in the current editied AO. Also there are some more ways to group AOs, like region, area, group, scene…)

My first approach used simply the flag names. But this did not work out. I need a unique identifier for each flag. That is necessary cause Actionable Objects Flag Dependencies can depend on flags from other AOs. Also to be able to clone an Actionable Object easily, for example windows or doors, you need to be able to create the flags with the same name but with another unique identifier.

Also there was one small problem I solved for this to work : Since there can be more than one Action in an AO logically, the menu in each of these has to report to the AO which Action actually used a menu to create a new flag dependency.

I solved this by adding the unique property path to the return value of the selected menu item.

flagNameToAdd = property.propertyPath + "\n" + flagNameToAdd;

Here I still use the flag name instead of the flag uniquie uint identifier.

Then the next „trick“ I used is to place the selected menu item string into an member of the FlagDependencyDrawer. In the next OnGUI the drawer checks if the member variable is empty, if not, it parses the content and creates the Flag Property accordingly. It also makes sure that the string is for this drawer, which seems a bit redundent, but seemed necessary at the time.

if (property.propertyPath == propertyPath)

2021. Happy new yeah.

While still working on perfecting my Interaction Project, a little framework to easily create interaction between objects based on a easy to manage flag system, I am running into old problems when creating a WebGL Build for the current mini-game.

  1. The script at (localhost:x) embroid1.framework.js.gz was loaded, even if its MIME-Typ („application/gzip“) is no valid MIME-Type for JavaScript
  2. Uncaught SyntaxError: illegal character U+001F embroid1.framework.js.gz:1
  3. Uncaught ReferenceError: unityFramework is not defined onload http://localhost:x/Build/embroid1.loader.js:1

I had this before. I solved it. But it was quite a long time ago last year.

I will find the solution again and then post it here.

Also hopefully more posts about my Interaction Project this month and some cc0 textures / materials I made with the Substance Suite, with which I have fallen in love.

The flag of Interaction – more about the FlagSystem of Interaction Project

To speed up the whole flag system handling, I wrote a custom inspector for the ActionableObject class.

The flags for the Actionable Object can easily be created/deleted in the custom inspector. Also the initial status can be set.

Important is here that by creating/deleting Flags for that ActionableObject they automatically get registered in the Flag System Manager.

With this other ActionableObjects as well every other Object that uses the flag system can access the flags in their Actions.

Every AO or any object that wants to use Flags need a FlagDependency List.

A custom property drawer for it handles everything inside it, so there is no need for any calls to the class which the FlagDependency List is part of.

But how to keep track of all the flags of all objects ? Here comes the „Add Flag Dependency“ Button into play. This button opens a menu that has a list of all available flags for all Objects that have registered flags.

So much for that now.

Footsteps on terrain.

While the basic system of the InterAction Project is now working fine and bug free (as far as I can tell) and with A LOT of error preventing checks and feedback on them (no automated test for now, but this will come), I actually try to implement footsteps on terrain.

For normal meshes I have done a working solution with a custom FeetSoundMaterial.

I also found a way to get the dominant texture at the position of the terrain where the player (or any other moving entity) stands as an index.

BUT since I am using the experimental terrain tools, I was curious if I could use the enhanced layer system of the terrain tools package.

I drove deep into the source of the terrain tool packages, actually very interesting.

Would it not be cool if I just could enhance the scriptables Layer and add there my footstep audio files as list ? Then I just would need to retrieve the layer (not the TerrainLayer, that class is a sealed object, so I sadly can not make a derived class from it, which would be the coolest way.)

But I would have to change a lot in the editor implementation of the terrain tools to be able to use my own Layer scriptables. So it seems I have to go the boring way and just make another class which combines the TerrainLayer index with the fitting footsteps. Which also means I have to keep track of the indexes and changes there by hand, which is very boring.

Make some noise (with your feet) (InterAction Project Status Update)

The InterAction Project has two different kinds of Player steering modes : One for Adventure-like games and one that is more the classic FPS steering. But despite which is used : You probably need footsteps.

The character controller I am writing for the InterAction Project (which of course does not have to be used) makes use of a set of components that provide foot step sounds, depending on the material the player is walking on.

For now it is supporting only meshes, but the terrain support is nearly done.

If you want to have foot steps while moving on a surface, you simply assign a FeetSoundMaterial component to it. Then you can assing multiple footstep sounds to this Material. Everything else is done by the FeetSoundManager component attached to the player.

The feet sound manager casts a ray down to the surface and retrieves the FeetSoundMaterial. It then takes the AudioClip[] array it gets from the FeetSoundMaterial and plays a random footstep sound IF no other footstep is playing.

Of course this could be done much better. Like a library of footsteps groups and then using the mesh material to select which group to use, just as an example.

But for now this works fine. 😀

[InterAction Project] Quest Nodes and FlagDependentObstacles

After hardening the whole system and capseling it into its own project, I can move on with other needed functionalites for it. Ah yes, also I versioned it with git, since my ultimate goal is it, to publish InterAction System on GitHub.

I needed for my adventure game the Quest Nodes and obstacles. The Quest Nodes (you could also call them location triggers) were allready in places, but not using the Flag System.

A QuestNode is a box collider set to a trigger, that, for now plays a sound and shows a message, optionally depending on flag settings.

A FlagDependent Obstacle is basicly the same, but instead of triggern it blocks passage depending on flags.

More about Actionable Objects. ( InterAction Project Series )

Let’s have a look how this little system I am doing enables the player to open and close this power block door, as a simple example.

The orange door you see in the scene view in the picture above has a component called ActionableObject. If the layer „ActionableObjects“ exists, a GameObject with this component on it will be set to this layer. This is needed because the raytracing of the EyeModule only hits on objects in this layer. (If the layer does not exist, errors will be thrown, but there is a convient editor menu where you can create all needed layers without having to add them manually.)

„More about Actionable Objects. ( InterAction Project Series )“ weiterlesen