【问题标题】:Why i can't see the functions from my script in Unity?为什么我在 Unity 中看不到脚本中的函数?
【发布时间】:2020-07-22 12:05:45
【问题描述】:

我想使用带有 onclick 事件的脚本,但每当我将它拖到 Unity 时,我都看不到我在该脚本中设置的功能。我只能看到 defaultasset 功能。其他脚本多次发生这种情况。可能是什么问题? 对不起,如果这是一个愚蠢的问题。我对 Unity 和 C# 非常陌生。

我想使用脚本中的这个函数:

 public void OpenPDF(string PDFNameWithExtension)
    {
        StartCoroutine(SaveTemplatePDF(PDFNameWithExtension, "application/pdf"));
    }   

Assets 是 Streaming Assets 的父目录

如果有帮助,请提供完整的脚本。如您所见,它只是一个打开 pdf 或图像的脚本:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityAndroidOpenUrl;
using UnityEngine;

public class DemoScript : MonoBehaviour
{
    private const string MAIN_DIR = "/storage/emulated/0";
    private const string testFolderName = "TotalPower";
    // private const string PDFName = "template.pdf";

    private static string pathToTestFolder;
    private static string pathToTemplatePDF;

    private void Start()
    {
        pathToTestFolder = Path.Combine(MAIN_DIR, testFolderName);
        if (!Directory.Exists(pathToTestFolder))
        {
            Directory.CreateDirectory(pathToTestFolder);
        }
    }


    public void OpenPDF(string PDFNameWithExtension)
    {
        StartCoroutine(SaveTemplatePDF(PDFNameWithExtension, "application/pdf"));
    }

    public void OpenImage(string imageNameWithExtension)
    {
        StartCoroutine(SaveTemplatePDF(imageNameWithExtension, "application/png"));
    }


    private IEnumerator SaveTemplatePDF(string PDFName, string dataType)
    {

        string localPathToTemplatePDF = Path.Combine(Application.streamingAssetsPath, PDFName);
        WWW www = new WWW(localPathToTemplatePDF);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError("Error while loading template PDF: " + www.error);
            yield break;
        }
        pathToTemplatePDF = Path.Combine(pathToTestFolder, PDFName);
        File.WriteAllBytes(pathToTemplatePDF, www.bytes);

        AndroidOpenUrl.OpenFile(pathToTemplatePDF, dataType);
    }
}

【问题讨论】:

标签: c# unity3d


【解决方案1】:

我通过使用我在 github 上找到的以下插件解决了这个问题:https://github.com/Mihail5412/Unity-Android-Files-Opener

我的项目缺少 AndroidOpenUrl.cs,这就是它不起作用的原因

此外,脚本不应位于 StreamingAssets 文件夹中,而应仅位于您要显示的文件中

【讨论】:

    猜你喜欢
    • 2019-08-09
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多