Jump to content

[Solved] How to colorize an existing Image-type asset?


Recommended Posts

(pinging @DMagic for comments as he clearly has extensive knowledge on icons like this post)

More particularly, I am trying to colorize a vessel's map icon in the tracking station or mapview scene.

Here're the codes below that I got to work but...

//You should be able to use MonoBehaviour to run the codes related to MapNode during Update()

public class CNCCommNetUI : CommNetUI
{
  MapObject m_MapObject;

  public CNCCommNetUI()
  {
    base.colorHigh = new Color(0f, 0f, 1f, 1f); // blue connections
  }
  
  protected override void Start()
  {
  	base.Start();
  	GameEvents.onPlanetariumTargetChanged.Add(OnPlanetariumTargetChanged);
  }
  
  protected override void UpdateDisplay()
  {
    base.UpdateDisplay();
    
    //attempt to access vessel's map sprite
    //m_MapObject is of MapObject class, obtained from 'private void OnPlanetariumTargetChanged(MapObject mapObject)'
    MapNode mn = m_MapObject.uiNode;
    MapNode new_mn = MapNode.Create(mn.mapObject, new Color(0f, 0f, 1f, 1f), 512, mn.Interactable, mn.Pinnable, mn.InputBlocking); // try to colorise the grey icon as blue
    m_MapObject.uiNode = new_mn;
  }
  
  private void OnPlanetariumTargetChanged(MapObject mapObject)
  {
    if (mapObject == null)
    {
    	return;
    }

    switch (mapObject.type)
    {
      case MapObject.ObjectType.Vessel:
      CNCLog.Debug("OnPlanetariumTargetChanged: Vessel: " + mapObject.vessel.vesselName);
      m_MapObject = mapObject;
      break;
    }
  }
}

tjGtdc7.jpg

The relay icon somehow turns into the different icon of an unknown type (generic?), and remains un-colorised.

The MapNode class (decompiled by VS's ILSpy addon) has a confusing maze of methods like NodeUpdate(), Terminate() etc. Does anyone know how to solve this puzzling question?

 

Edited by TaxiService
Link to comment
Share on other sites

The MapNode has a protected Image component, MapNode.img.

A lot of UI elements are either white or grey with the color added in later. To change that color you just need to set the color property of the Image: MapNode.img.color = ...

Since that field is protected you might need to go through the hassle of manually accessing it with the GetComponent<Image> or GetComponent(s)InChildren<Image> methods.

Sometimes Debug Stuff can be invaluable for figuring out the hierarchy of UI elements like this:

As for why it's changing to the generic map object type, I'm guessing there is something wrong with the first argument of the Create method. 

Edited by DMagic
Link to comment
Share on other sites

@DMagic

Thanks for the direct-access tip and the lovely debug mod. These two lines are sufficient to recolorize an Image-type asset.

066tqJP.png

//attempt to access vessel's map sprite
Image thisImageIcon = currentMapObject.uiNode.GetComponentInChildren<Image>();
thisImageIcon.color = new Color(1f, 0f, 1f, 1f); // pink

No further action is required such as invoking an update method after the said change is applied. KSP reads this image data in real-time when drawing graphics .

Edited by TaxiService
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...