public class JsonLoaderVideo
{
private const string FileName = "VideoCourse";
private const string ModifierPath = "StreamingAssets/Config";
code
private string jsonFilePath;
public VideoPaths LoadVideo()
{
jsonFilePath = Path.Combine(Application.dataPath, ModifierPath, FileName + ".json");
if (!File.Exists(jsonFilePath))
{
Debug.LogError($"JSON файл не найден: {jsonFilePath}");
CreateConfigPath();
Debug.LogWarning($"Создан новый пустой файл и заполнен стартовыми данными");
}
var jsonString = File.ReadAllText(jsonFilePath, Encoding.UTF8);
var videoPaths = JsonUtility.FromJson<VideoPaths>(jsonString);
return videoPaths;
}
private void CreateConfigPath()
{
var Paths = new VideoPaths();
Paths.videoData = new VideoData[1];
Paths.videoData[0] = new VideoData { name = "Video 1", path = "" };
var newFilePath = JsonUtility.ToJson(Paths);
File.WriteAllText(jsonFilePath, newFilePath, Encoding.UTF8);
}
}
[System.Serializable]
public class VideoPaths
{
public VideoData[] videoData;
}
[System.Serializable]
public class VideoData
{
public string name;
public string path;
}
Assistant
The above code is a C# class, named JsonLoaderVideo, that is responsible for loading and parsing a JSON file that contains video paths.
Here is how the code works:
The JsonLoaderVideo class has a constant string FileName which represents the name of the JSON file ("VideoCourse") and a constant string ModifierPath which represents the relative path to the JSON file ("StreamingAssets/Config").
The class also has a private string jsonFilePath which will hold the full path to the JSON file.
The LoadVideo method is used to load the video paths from the JSON file. It first combines the Application.dataPath, ModifierPath, and FileName to get the full path to the JSON file. Then, it checks if the file exists. If the file doesn't exist, it logs an error message and calls the CreateConfigPath method to create a new empty JSON file with some starter data. If the file exists, it reads the JSON contents using File.ReadAllText and parses it into an instance of the VideoPaths class using JsonUtility.FromJson. Finally, it returns the parsed VideoPaths object.
The CreateConfigPath method is called when the JSON file doesn't exist. It creates a new instance of the VideoPaths class, initializes the videoData array with a single element, and sets the name and path properties of the VideoData object. It then converts the VideoPaths object to a JSON string using JsonUtility.ToJson and writes it to the JSON file using File.WriteAllText.
The VideoPaths and VideoData classes are used to represent the structure of the JSON data. VideoPaths has a property videoData which is an array of VideoData objects. VideoData has properties name and path which represent the name and path of a video file.
Overall, this code is a utility class that provides methods for loading and parsing video paths from a JSON file. It also handles the case when the JSON file doesn't exist by creating a new file with some starter data.