You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检查项目中是否安装包?如何编写TextMeshPro存在判断的伪代码

Hey there! Let's break down your two questions step by step—they’re both super common when building flexible Unity modules that play nice with different project setups.


1. Checking if a Package is Installed in Unity

There are two main scenarios here: checking during editor time (for tools/setup scripts) and checking at runtime (for your game/module logic).

Editor-Time Check (Using Package Manager API)

If you're writing an editor script, you can directly query Unity's Package Manager to see if a package is installed. This is the most accurate method for editor workflows:

using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEngine;

public static class PackageChecker
{
    public static bool IsPackageInstalled(string packageFullName)
    {
        // Fetch the list of installed packages
        var listRequest = Client.List();
        
        // Wait for the request to complete (safe for editor use)
        while (!listRequest.IsCompleted)
        {
            if (listRequest.Status == StatusCode.Failure)
            {
                Debug.LogError($"Failed to fetch package list: {listRequest.Error.message}");
                return false;
            }
        }

        // Check if the package exists and is installed
        foreach (var package in listRequest.Result)
        {
            if (package.name == packageFullName && package.status == PackageStatus.Installed)
            {
                return true;
            }
        }

        return false;
    }
}

To check for TextMeshPro specifically, call it with the official package name:

bool tmpInstalled = PackageChecker.IsPackageInstalled("com.unity.textmeshpro");

Runtime Check (Using Type Reflection)

For runtime code (since you can't use Editor APIs in builds), the safest way is to try loading the package's key type dynamically. If the type exists, the package is installed and available:

public static bool IsTextMeshProAvailableAtRuntime()
{
    // Try to load the TMP_Text type; returns null if not found
    Type tmpTextType = Type.GetType("TMPro.TMP_Text, UnityEngine.TextMeshPro");
    return tmpTextType != null;
}

2. Pseudo-Code & Implementation for Fallback Text Component Lookup

First, let's outline the logic in simple pseudo-code to make the flow clear:

FUNCTION DoesTextMeshProExist()
    ATTEMPT TO GET TYPE "TMPro.TMP_Text"
    RETURN TYPE IS NOT NULL

FUNCTION FindAllTextFieldsInScene()
    textComponents = EMPTY LIST
    
    IF DoesTextMeshProExist()
        // Grab all TextMeshPro text components
        textComponents = GET ALL OBJECTS OF TYPE "TMPro.TMP_Text" (including inactive)
    ELSE
        // Grab standard UI Text + 3D Text Mesh components
        textComponents = GET ALL OBJECTS OF TYPE "UnityEngine.UI.Text"
        ADD ALL OBJECTS OF TYPE "UnityEngine.TextMesh" TO textComponents
    
    RETURN textComponents

Actual C# Implementation

To avoid compile errors when TextMeshPro isn't installed, we'll use reflection for the TMP lookup. Here's a working, runtime-safe version:

using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;

public static class TextComponentFinder
{
    // Check if TextMeshPro is available
    private static bool IsTextMeshProAvailable()
    {
        return Type.GetType("TMPro.TMP_Text, UnityEngine.TextMeshPro") != null;
    }

    public static List<Component> FindAllTextComponentsInScene(bool includeInactive = true)
    {
        List<Component> textComponents = new List<Component>();

        if (IsTextMeshProAvailable())
        {
            // Use reflection to find TMP_Text components without direct references
            Type tmpTextType = Type.GetType("TMPro.TMP_Text, UnityEngine.TextMeshPro");
            var tmpTexts = GameObject.FindObjectsOfType(tmpTextType, includeInactive);
            
            foreach (var tmpText in tmpTexts)
            {
                textComponents.Add(tmpText as Component);
            }
        }
        else
        {
            // Fallback to standard text components
            var uiTexts = GameObject.FindObjectsOfType<Text>(includeInactive);
            textComponents.AddRange(uiTexts);

            var textMeshes = GameObject.FindObjectsOfType<TextMesh>(includeInactive);
            textComponents.AddRange(textMeshes);
        }

        return textComponents;
    }
}

Key Notes:

  • Reflection is safe here: By using Type.GetType() instead of direct using TMPro;, your code will compile even if TextMeshPro isn't installed in the project.
  • Include inactive objects: The includeInactive flag ensures you don't miss text fields that are disabled in the scene.
  • Editor vs Runtime: If you're using this in an editor tool, you could combine the editor package check with the reflection logic for maximum accuracy.

内容的提问来源于stack exchange,提问作者likuku

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:06:07