playMaker

Author Topic: Custom Action - Either or Component  (Read 1110 times)

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Custom Action - Either or Component
« on: April 25, 2017, 02:07:49 PM »
I have a playmaker action that could be for 1 of 2 different component (scripts). In this case

1. textmeshpro
2. textmeshpro UGUI

My script does a simple check and then runs the nessesary function if/else the script is attached (I will improve this with an error check after, and use if, if else, else).

However, the problem is that

A. Its ugly. Is there a better way to do this? If else is ok, but I feel I am doing too much code in the if else?

For void update(), I need to do the check again? (for everyframe options).

Any ideas?

Code: [Select]
// (c) Eric Vander Wal, 2017 All rights reserved.
// Custom Action by DumbGameDev
// www.dumbgamedev.com

using UnityEngine;
using TMPro;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory("TextMesh Pro Basic")]
    [Tooltip("Set Text Mesh Pro Text.")]

public class  setTextmeshProText : FsmStateAction
{
[RequiredField]
[Tooltip("Textmesh Pro component is required.")]
public FsmOwnerDefault gameObject;

[RequiredField]
[UIHint(UIHint.TextArea)]
[TitleAttribute("Textmesh Pro Text")]
[Tooltip("The text for Textmesh Pro.")]
public FsmString textString;

[Tooltip("Check this box to preform this action every frame.")]
public FsmBool everyFrame;

// for textmesh pro
TextMeshPro meshproScript;
// for textmesh pro UGUI
TextMeshProUGUI meshproUguiScript;

public override void Reset()
{

gameObject = null;
textString = null;
everyFrame = false;
}

public override void OnEnter()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);

TextMeshPro tmp = go.GetComponent<TextMeshPro>();

// if textmesh pro normal script attached, do this
if (tmp != null)
{
Debug.Log("TextMesh Pro Script Attached");
meshproScript = go.GetComponent<TextMeshPro>();

if (!everyFrame.Value)
{
DoMeshChange();
Finish();
}

}

// if textmesh pro Ugui is attached
else
{
Debug.Log ("TextMesh Pro UGUI Script Attached");
meshproUguiScript = go.GetComponent<TextMeshProUGUI>();

if (!everyFrame.Value)
{
DoMeshChangeUgui();
Finish();
}
}

}


// textmesh pro normal script
void DoMeshChange()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

meshproScript.text = textString.Value;
}


// textmesh pro ugui script
void DoMeshChangeUgui()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

meshproUguiScript.text = textString.Value;
}

}
}